Spring零基礎(chǔ)入門IOC
1.HelloSpring
導(dǎo)入Jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
1、編寫一個(gè)Hello實(shí)體類
public class Hello {
private String str;
public Hello(String str) {
this.str = str;
}
public Hello() {
}
public String getStr() {
return str;
}
//set方法是核心
public void setStr(String str) {
this.str = str;
}
@Override
public String toString() {
return "Hello{" +
"str='" + str + '\'' +
'}';
}
}2、編寫我們的spring文件 , 這里我們命名為beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="com.hc.pojo.Hello">
<property name="str" value="Spring"/>
</bean>
<!-- 使用spring來創(chuàng)建對(duì)象,在spring這些都稱為Bean-->
<!-- 類型 變量名 =new 類型();-->
<!-- Hello hello=new Hello();-->
</beans>
3、我們可以去進(jìn)行測試了 .
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Object hello = context.getBean("hello");
System.out.println(hello.toString());
}
}思考
- Hello 對(duì)象是誰創(chuàng)建的 ? hello 對(duì)象是由Spring創(chuàng)建的
- Hello 對(duì)象的屬性是怎么設(shè)置的 ? hello 對(duì)象的屬性是由Spring容器設(shè)置的
這個(gè)過程就叫控制反轉(zhuǎn) :
- 控制 : 誰來控制對(duì)象的創(chuàng)建 , 傳統(tǒng)應(yīng)用程序的對(duì)象是由程序本身控制創(chuàng)建的 , 使用Spring后 , 對(duì)象是由Spring來創(chuàng)建的
- 反轉(zhuǎn) : 程序本身不創(chuàng)建對(duì)象 , 而變成被動(dòng)的接收對(duì)象 .
依賴注入 : 就是利用set方法來進(jìn)行注入的.
IOC是一種編程思想,由主動(dòng)的編程變成被動(dòng)的接收
可以通過newClassPathXmlApplicationContext去瀏覽一下底層源碼 .
修改案例一
我們?cè)诎咐恢校?新增一個(gè)Spring配置文件beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mysqlImpl" class="com.hc.dao.UserDaoMysqlImpl"/>
<bean id="OracleImpl" class="com.hc.dao.UserDaoOracleImpl"/>
<!-- 使用spring來創(chuàng)建對(duì)象,在spring這些都稱為Bean-->
<!-- 類型 變量名 =new 類型();-->
<!-- Hello hello=new Hello();-->
<bean id="UserServiceImpl" class="com.hc.service.UserServiceImpl">
<property name="userDao" ref="OracleImpl"/>
</bean>
</beans>
測試!
import com.hc.dao.UserDaoMysqlImpl;
import com.hc.dao.UserDaoOracleImpl;
import com.hc.dao.UserDaoSqlserverImpl;
import com.hc.service.UserService;
import com.hc.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import sun.security.mscapi.CPublicKey;
/**
* @author HC
* @version 1.0
*/
public class MyTest {
public static void main(String[] args) {
//用戶調(diào)用的是業(yè)務(wù)層,dao層他們不需要接觸!
// UserServiceImpl userService = new UserServiceImpl();
// userService.setUserDao(new UserDaoSqlserverImpl());
// userService.setUserDao(new UserDaoOracleImpl());
// userService.getUser();
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");
userServiceImpl.getUser();
}
}OK , 到了現(xiàn)在 , 我們徹底不用再程序中去改動(dòng)了 , 要實(shí)現(xiàn)不同的操作 , 只需要在xml配置文件中進(jìn)行修改 , 所謂的IoC,一句話搞定 : 對(duì)象由Spring 來創(chuàng)建 , 管理 , 裝配 !
2.IOC創(chuàng)建對(duì)象方式
2.1.通過無參構(gòu)造方法來創(chuàng)建
通過無參構(gòu)造方法來創(chuàng)建
1、User.java
public class User {
private String name;
public User(String name) {
this.name = name;
}
// public User() {
// }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show()
{
System.out.println("name="+getName());
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}2、beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--一、無參-->
<!-- <bean id="user" class="com.kuang.pojo.User">-->
<!-- <property name="name" value="hc"/>-->
<!-- </bean>-->
<!-- //二、有參-->
<!-- <bean id="user" class="com.kuang.pojo.User">-->
<!-- <constructor-arg index="0" value="狂神說java"/>-->
<!-- </bean>-->
<!-- 三、有參-->
<!-- <bean id="user" class="com.kuang.pojo.User">-->
<!-- <constructor-arg name="name" value="hc"/>-->
<!-- </bean>-->
<bean id="user" class="com.kuang.pojo.UserT" name="u1 u2,u3">
<property name="name" value="西部開源"/>
</bean>
<!-- alias取別名-->
<!-- <alias name="user" alias="ccccccccccccccccc"/>-->
</beans>3、測試類
public class MyTest {
public static void main(String[] args) {
// User user = new User();
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//User user1 = (User) context.getBean("user");
// User user1 = (User) context.getBean("ccccccccccccccccc");
UserT user4 = (UserT) context.getBean("u2");
// User user2 = (User) context.getBean("user");
user4.show();
//System.out.println(user1==user2);
}
}結(jié)果可以發(fā)現(xiàn),在調(diào)用show方法之前,User對(duì)象已經(jīng)通過無參構(gòu)造初始化了!
2.2.通過有參構(gòu)造方法來創(chuàng)建
通過有參構(gòu)造方法來創(chuàng)建
1、UserT . java
public class UserT {
private String name;
public UserT() {
System.out.println("UserT被創(chuàng)建了");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show()
{
System.out.println("name="+getName());
}
}2、beans.xml 有三種方式編寫
<!--一、無參--> <!-- <bean id="user" class="com.kuang.pojo.User">--> <!-- <property name="name" value="hc"/>--> <!-- </bean>--> <!-- //二、有參--> <!-- <bean id="user" class="com.kuang.pojo.User">--> <!-- <constructor-arg index="0" value="狂神說java"/>--> <!-- </bean>--> <!-- 三、有參--> <!-- <bean id="user" class="com.kuang.pojo.User">--> <!-- <constructor-arg name="name" value="hc"/>--> <!-- </bean>-->
3、測試
public class MyTest {
public static void main(String[] args) {
// User user = new User();
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//User user1 = (User) context.getBean("user");
// User user1 = (User) context.getBean("ccccccccccccccccc");
UserT user4 = (UserT) context.getBean("u2");
// User user2 = (User) context.getBean("user");
user4.show();
//System.out.println(user1==user2);
}
}結(jié)論:在配置文件加載的時(shí)候。其中管理的對(duì)象都已經(jīng)初始化了!
到此這篇關(guān)于Spring零基礎(chǔ)入門IOC的文章就介紹到這了,更多相關(guān)Spring IOC內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot使用Spring Security實(shí)現(xiàn)登錄注銷功能
這篇文章主要介紹了SpringBoot使用Spring Security實(shí)現(xiàn)登錄注銷功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-09-09
關(guān)于Spring MVC同名參數(shù)綁定問題的解決方法
Spring MVC中的參數(shù)綁定還是蠻重要的,最近在使用中遇到了同名參數(shù)綁定的問題,想著總結(jié)分享出來,下面這篇文章主要給大家介紹了關(guān)于Spring MVC同名參數(shù)綁定問題的解決方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08
IDEA 2021配置JavaWeb項(xiàng)目超詳細(xì)教程
本文通過圖文并茂的形式給大家介紹IDEA 2021配置JavaWeb項(xiàng)目的過程,內(nèi)容簡單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-08-08
Java編程實(shí)現(xiàn)高斯模糊和圖像的空間卷積詳解
這篇文章主要介紹了Java編程實(shí)現(xiàn)高斯模糊和圖像的空間卷積詳解,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
springboot 在ftl頁面上使用shiro標(biāo)簽的實(shí)例代碼
這篇文章主要介紹了springboot 在ftl頁面上使用shiro標(biāo)簽的實(shí)例代碼,通過文字說明結(jié)合實(shí)例的形式給大家介紹的非常詳細(xì),需要的朋友參考下吧2018-05-05
springboot?通過博途獲取plc點(diǎn)位的數(shù)據(jù)代碼實(shí)現(xiàn)
這篇文章主要介紹了springboot?通過博途獲取plc點(diǎn)位的數(shù)據(jù)的代碼實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08
Java數(shù)據(jù)結(jié)構(gòu)和算法之鏈表詳解
鏈表是一種物理存儲(chǔ)單元上非連續(xù)、非順序的存儲(chǔ)結(jié)構(gòu),java代碼實(shí)現(xiàn)單鏈表,插入,刪除和遍歷等功能,這篇文章主要給大家介紹了關(guān)于Java數(shù)據(jù)結(jié)構(gòu)和算法之鏈表的相關(guān)資料,需要的朋友可以參考下2024-01-01

