一篇文章帶你了解Java Spring基礎(chǔ)與IOC
About Spring
開源免費(fèi)框架,輕量級,非入侵式框架。Spring就是一個(gè)輕量級的控制反轉(zhuǎn)(IOC)和面向切片編程(AOP)的框架
Maven repo:Spring Web MVC + spring-jdbc(整合Mybatis)
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.9</version>
</dependency>
Spring兩大特點(diǎn)
- 控制反轉(zhuǎn)(IOC)
- 面向切片編程(AOP)支持事務(wù)處理
About IOC
控制反轉(zhuǎn):IOC是一種設(shè)計(jì)思想,通過描述(XML或注解)并通過第三方生產(chǎn)或獲取對象的方式。之前對象的創(chuàng)建與對象的依賴關(guān)系完全在java硬編碼程序中,控制權(quán)在程序;實(shí)現(xiàn)IOC后,控制權(quán)在第三方,實(shí)現(xiàn)降藕。
在Spring中實(shí)現(xiàn)控制反轉(zhuǎn)的是 IoC容器 ,實(shí)現(xiàn)方法是 依賴注入DI(Dependency Injection,DI)

引用狂神的一個(gè)例子簡單理解下
private UserDao userDao = null;
public UserServiceImpl(){
userDao = new UserDaoImpl();
}
在之前我們使用JavaWeb寫service=>dao的時(shí)候,是通過如上的方式去實(shí)現(xiàn)的,項(xiàng)目構(gòu)建大概如下

那么如果此時(shí)我的UserDao接口又了一個(gè)新的實(shí)現(xiàn)類暫且為 UserDaoImpls ,這個(gè)實(shí)現(xiàn)類中有新的功能實(shí)現(xiàn),那么就需要到UserServiceImpl中再去構(gòu)造方法加一段如下的代碼:
userDao = new UserDaoImpls();
那么這時(shí)如果該項(xiàng)目還沒發(fā)布那到無所謂,如果是已經(jīng)上線的項(xiàng)目是不可能這樣重新去修改代碼的,或者如果有n個(gè)new,就要修改n處。
解決這個(gè)問題就是通過一個(gè)set方法。如下:
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
在構(gòu)造方法中實(shí)例化對象的這個(gè)操作,改為利用set封裝并將需要new的實(shí)現(xiàn)類的名稱變?yōu)閟et方法的參數(shù),實(shí)現(xiàn)用戶可控的去new一個(gè)新的實(shí)現(xiàn)類從而添加新的功能展示到頁面。
而這個(gè)思想就是控制反轉(zhuǎn)(IOC)的原型,將new實(shí)現(xiàn)類對象的主動權(quán)交給了用戶而不是程序,從本質(zhì)上解決了上面的問題,也實(shí)現(xiàn)了降藕。
Hello Spring
Hello.java
package com.zh1z3ven.pojo;
public class Hello {
private String str;
public Hello() {
}
public Hello(String str) {
this.str = str;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
@Override
public String toString() {
return "Hello{" +
"str='" + str + '\'' +
'}';
}
}
Beans.xml
一個(gè)bean標(biāo)簽代表一個(gè)對象, id代表在Spring中這個(gè)類要實(shí)例化生成的對象的名字, class指定這個(gè)實(shí)體類
設(shè)置對象的屬性值
引用Spring容器中創(chuàng)建的對象
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--使用Spring創(chuàng)建對象,Spring都稱之為bean-->
<!-- 一個(gè)bean標(biāo)簽代表一個(gè)對象, id代表要實(shí)例化的對象的名字, class指定這個(gè)實(shí)體類-->
<bean id="hello" class="com.zh1z3ven.pojo.Hello">
<!-- 設(shè)置實(shí)體類的屬性值-->
<property name="str" value="Spring"/>
</bean>
</beans>
Test.java
在Spring中也存在一個(gè)上下文,通過 ApplicationContext context = new ClassPathXmlApplicationContext(“beans.xml”); 傳入xml配置文件名字來獲取該xml文件的上下文對象。利用 ApplicationContext#getBean() 傳入在xml中配置的該類的id獲取該實(shí)體類對象。
public class MyTest {
public static void main(String[] args) {
//獲取Spring上下文對象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//從Spring中取出對象
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello);
}
}
IOC創(chuàng)建對象的幾種方式
PS:在配置文件加載的時(shí)候,通過bean標(biāo)簽注冊的對象就已經(jīng)在Spring IoC容器中初始化了。
總體來說就兩種方式:
無參構(gòu)造
默認(rèn)使用
有參構(gòu)造
- 下標(biāo)賦值
<!--通過下標(biāo)賦值-->
<bean id="hello2" class="com.zh1z3ven.pojo.Hello">
<constructor-arg index="0" value="Spring2"/>
</bean>
- 類型復(fù)制
<!--通過類型賦值-->
<bean id="hello3" class="com.zh1z3ven.pojo.Hello">
<constructor-arg type="java.lang.String" value="Spring3"/>
</bean>
- 屬性名賦值
<!--屬性名賦值-->
<bean id="hello4" class="com.zh1z3ven.pojo.Hello">
<constructor-arg name="str" value="Spring4"/>
</bean>
Spring import settings
import標(biāo)簽可導(dǎo)入其他beans.xml配置文件,而applicationContext.xml到時(shí)可作為總bean的配置文件,而不需要導(dǎo)入多個(gè)xml

Dependency Injection
依賴注入(Dependency Injection,DI)
PS:一定需要pojo中實(shí)現(xiàn)set才可以利用bean標(biāo)簽注入
1.構(gòu)造器注入
也就是上面提到的創(chuàng)建對象的方式,分為無參構(gòu)造和有參構(gòu)造
無參構(gòu)造
默認(rèn)使用
有參構(gòu)造
- 下標(biāo)賦值
<!--通過下標(biāo)賦值-->
<bean id="hello2" class="com.zh1z3ven.pojo.Hello">
<constructor-arg index="0" value="Spring2"/>
</bean>
- 類型復(fù)制
<!--通過類型賦值-->
<bean id="hello3" class="com.zh1z3ven.pojo.Hello">
<constructor-arg type="java.lang.String" value="Spring3"/>
</bean>
- 屬性名賦值
<!--屬性名賦值-->
<bean id="hello4" class="com.zh1z3ven.pojo.Hello">
<constructor-arg name="str" value="Spring4"/>
</bean>
2.set注入
依賴:bean對象的注入依賴于Spring容器
注入:bean對象的屬性,由容器來注入
3.拓展注入
Student.java
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private String apache;
private Properties info;
public String getApache() {
return apache;
}
public void setApache(String apache) {
this.apache = apache;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
beans.xml
<bean id="address" class="com.zh1z3ven.pojo.Address">
<property name="address" value="beijing"/>
</bean>
<bean id="student1" class="com.zh1z3ven.pojo.Student">
<!-- 普通屬性值注入-->
<property name="name" value="zh1z3ven"/>
<!-- ref 引用對象注入-->
<property name="address" ref="address"/>
<!-- 數(shù)組array注入-->
<property name="books">
<array>
<value>紅樓夢</value>
<value>西游記</value>
<value>水滸傳</value>
<value>三國演義</value>
</array>
</property>
<!-- List注入-->
<property name="hobbys">
<list>
<value>聽音樂</value>
<value>看電影</value>
<value>敲代碼</value>
<value>寫文章</value>
</list>
</property>
<!-- Map注入-->
<property name="card">
<map>
<entry key="銀行卡" value="1"></entry>
<entry key="身份證" value="2"></entry>
<entry key="學(xué)生證" value="3"></entry>
<entry key="電話卡" value="4"></entry>
<entry key="校園卡" value="5"></entry>
</map>
</property>
<!-- Set注入-->
<property name="games">
<set>
<value>LOL</value>
<value>CF</value>
<value>qq</value>
</set>
</property>
<!-- null注入-->
<property name="wife">
<null></null>
</property>
<!-- 空值注入-->
<property name="apache" value=""/>
<!-- properties-->
<property name="info">
<props>
<prop key="id">10</prop>
<prop key="city">北京</prop>
</props>
</property>
</bean>
P-namespcae&C-namespace
1.p命名空間注入,可以直接注入屬性值,類似于bean標(biāo)簽中property-name-value
Beans.xml頭部需導(dǎo)入
xmlns:p="http://www.springframework.org/schema/p"
<bean id="user1" class="com.zh1z3ven.pojo.User" p:name="zh1z3ven1" p:age="18"/>
2.c命名空間注入,通過構(gòu)造器注入,類似于construct-args(需要實(shí)現(xiàn)有參構(gòu)造)
xmlns:c="http://www.springframework.org/schema/c"
<bean id="user2" class="com.zh1z3ven.pojo.User" c:name="zh1z3ven2" p:age="19"/>
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user1 = context.getBean("user1", User.class);
System.out.println(user1.getName());
System.out.println(user1.getAge());
User user2 = context.getBean("user2", User.class);
System.out.println(user2.getName());
System.out.println(user2.getAge());
}
Bean scopes
bean的作用域

singleton
默認(rèn)bean為scope = singleton單例模式運(yùn)行的
顯示定義:<bean id="user1" class="com.zh1z3ven.pojo.User" p:name="zh1z3ven1" p:age="18" scope="singleton"/>

單例模式,共享一個(gè)對象,比如如下例子,getBean指向的是同一個(gè)bean,那么在Spring IoC容器中僅僅會生成一個(gè)"user2"對象保存在內(nèi)存中,當(dāng)調(diào)用ApplicationContext.getBean(“user2”)時(shí)返回該對象
<bean id="user2" class="com.zh1z3ven.pojo.User" c:name="zh1z3ven2" c:age="19"/>
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user1 = context.getBean("user2", User.class);
User user2 = context.getBean("user2", User.class);
System.out.println(user1==user2);
}
prototype
原型模式prototype與singleton不同,每次上下文去getBean()時(shí)都會在Spring IoC容器內(nèi)創(chuàng)建一次該對象

還是拿上面的測試代碼,可以發(fā)現(xiàn)已經(jīng)不是同一個(gè)對象了,有點(diǎn)類似于多態(tài)
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user1 = context.getBean("user2", User.class);
User user2 = context.getBean("user2", User.class);
System.out.println(user1==user2);
}
而其余的生命周期在Web中才會遇到。
Bean的自動裝配
- 在xml顯示配置bean
- 在java代碼中配置bean
- 隱式自動裝配bean【autowire】
byName autowire
會在容器上下文中自動查找,和自己對象set方法后面的值對應(yīng)的beanid。也就是這里會去上下文中尋找有無"cat"這個(gè)beanid,有則自動裝配,如果并沒有匹配上,比如此時(shí)beanid被修改為了"cat123" 就會跑出異常。


byType autowire
會在容器上下文中尋找該類型與屬性值所對應(yīng)的類型相同的bean,基于bean中的class(需要在上下文中所有類型各自只出現(xiàn)一次)
<bean id="people" class="com.zh1z3ven.pojo.People" autowire="byType">
<property name="name" value="zh1z3ven"/>
</bean>
小結(jié)
- byname需要保證所有bean的id唯一,且這個(gè)bean的id的值要和需要自動裝配依賴注入的set方法的值一致。
- bytype需要保證所有bean的class唯一,且這個(gè)bean的class的值需要和set方法的值的類型一致。
注解實(shí)現(xiàn)自動裝配
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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
@Autowired
默認(rèn)使用byname方式去自動裝配,它可以對類成員變量、方法及構(gòu)造函數(shù)進(jìn)行標(biāo)注,完成自動裝配的工作。
在使用@Autowired時(shí),首先在容器中查詢對應(yīng)類型的bean(bytype)
如果查詢結(jié)果剛好為一個(gè),就將該bean裝配給@Autowired指定的數(shù)據(jù)
如果查詢的結(jié)果不止一個(gè),那么@Autowired會根據(jù)名稱來查找。(byname)
如果查詢的結(jié)果為空,那么會拋出異常。解決方法時(shí),使用@Autowried(required=false)
public class People {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
@Qualifier
如果存在多個(gè)類型且該類型有多個(gè)不同名字的對象,那么只用@Autowired會找不到該對象,可以配合@Qualifier(value="")來指定一個(gè)裝配的值。
@Autowired @Qualifier(value="dog222")
@Resource
java自帶的一個(gè)注解,和@Autowired,@Qualifier組合用法和效果基本是一樣
javax.annotation.Resource @Resource //不指定名稱自動裝配 @Resource(name="") //指定名稱自動裝配
使用注解開發(fā)
bean在xml里注冊,屬性值通過注解注入
@component
泛指各種組件,把普通pojo實(shí)例化到spring容器中,相當(dāng)于配置文件中的bean,將該類在配置文件下注冊到Spring容器中裝配bean。類似的還有:
1、@controller 控制器(注入服務(wù)) 用于標(biāo)注控制層,相當(dāng)于struts中的action層
2、@service 服務(wù)(注入dao) 用于標(biāo)注服務(wù)層,主要用來進(jìn)行業(yè)務(wù)的邏輯處理
3、@repository(實(shí)現(xiàn)dao訪問) 用于標(biāo)注數(shù)據(jù)訪問層,也可以說用于標(biāo)注數(shù)據(jù)訪問組件,即DAO組件
##@Scope
生命周期,用法:在目標(biāo)類上面聲明
@Scope("singleton")
@Configuration
用于聲明這是一個(gè)配置類
@Bean
相當(dāng)于在配置文件中注冊bean
方法名為之前的id屬性
方法返回值為之前的class屬性
@Import
導(dǎo)入其他配置類,等價(jià)于
<import resource="beans.xml"/>
使用方法
@Import(Config.class)
示例代碼
pojo
//相當(dāng)于在bean中注冊,相當(dāng)于在Spring IoC容器new一個(gè)對象
@Component
public class User {
@Value("CoLoo")
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
config
@Configuration
@ComponentScan("com.zh1z3ven.pojo")
public class AppConfig {
@Bean
public User getUser(){
return new User();
}
}
test
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
User user = context.getBean("user", User.class);
System.out.println(user.getName());
}
}
以上就是一篇文章帶你了解Java Spring基礎(chǔ)與IOC的詳細(xì)內(nèi)容,更多關(guān)于Java Spring與IOC的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring中事務(wù)管理的四種方法(銀行轉(zhuǎn)賬為例)
這篇文章主要給大家介紹了關(guān)于Spring中事務(wù)管理的四種方法,文中是以銀行轉(zhuǎn)賬為例,通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
Spring?Boot集成RabbitMQ以及隊(duì)列模式操作
RabbitMQ是實(shí)現(xiàn)AMQP(高級消息隊(duì)列協(xié)議)的消息中間件的一種,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot集成RabbitMQ以及隊(duì)列模式操作的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
mybatis spring配置SqlSessionTemplate的使用方式
這篇文章主要介紹了mybatis spring配置SqlSessionTemplate的使用方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Spring SpringMVC,Spring整合MyBatis 事務(wù)配置的詳細(xì)流程
這篇文章給大家介紹SSM整合詳細(xì)流程步驟 Spring SpringMVC,Spring整合MyBatis 事務(wù)配置,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-10-10
解決IDEA報(bào)錯(cuò),無效的源發(fā)行版 無效的目標(biāo)發(fā)行版:22問題
在項(xiàng)目編譯過程中,可能會出現(xiàn)“無效的源發(fā)行版”或“無效的目標(biāo)發(fā)行版”的報(bào)錯(cuò)信息,原因通常是編譯使用的JDK版本與項(xiàng)目設(shè)置的發(fā)布版本不一致,解決這類問題的辦法是統(tǒng)一JDK版本,具體操作為:在IDE的項(xiàng)目設(shè)置中(如File->ProjectStructure->ProjectSettings)2024-10-10
從零開始讓你的Spring?Boot項(xiàng)目跑在Linux服務(wù)器
這篇文章主要給大家介紹了如何從零開始讓你的Spring?Boot項(xiàng)目跑在Linux服務(wù)器的相關(guān)資料,由于springboot是內(nèi)嵌了tomcat,所以可以直接將項(xiàng)目打包上傳至服務(wù)器上,需要的朋友可以參考下2021-11-11

