1.SpringIOC的本質
Spring ioc指的是控制反轉,IOC容器負責實體化、定位、配置應用程式中的物件及建立這些物件間的依賴,交由Spring容器統一進行管理,從而實作松耦合.(解耦)
控制反轉(ioc)他是一種設計思想
來源于狂神說--------------------->狂神說java,見解很細,很到位,

個人這個圖對ioc的解釋很到位,容易理解
2.Spring 入門
1.創建一個Maven專案
這里我就不在細說了,參考我之前的博客 環境搭建
2.配置依賴
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
3.撰寫代碼
1.準備測驗的物體類
public class User {
private String username;
private String password;
}
2.配置spring的xml檔案
命名applicationContext.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-4.1.xsd">
<!--bean就是一個java物件,由Spring創建和管理-->
<bean id="user" >
<!--set注入-->
<property name="username" value="https://www.cnblogs.com/sky1023/p/root"/>
<property name="password" value="https://www.cnblogs.com/sky1023/p/123"/>
<!--構造器注入-->
<!-- <constructor-arg name="username" value="https://www.cnblogs.com/sky1023/p/2"></constructor-arg>-->
<!-- <constructor-arg name="password" value="https://www.cnblogs.com/sky1023/p/1"></constructor-arg>-->
</bean>
</beans
-
bean相當于set方法創建物件
public void setUse(User user) { this.user = user; } -
依賴注入(Dependency Injection,DI)
- property 注入
- constructor-arg 注入
3.junit測驗
@Test
public void IocTest2(){
/*獲取Spring組態檔*/
ApplicationContext applicationContext= new ClassPathXmlApplicationContext("applicationContext.xml");
/*1.反射獲取*/
User bean = applicationContext.getBean(User.class);
/*2.bean--id獲取*/
User bean1 = (User) applicationContext.getBean("user");
System.out.println(bean+"-----"+bean.hashCode());
System.out.println(bean1+"-----"+bean1.hashCode());
}
- ClassPathXmlApplicationContext來獲取applicationContext.xml組態檔
- 在使用getBean()來獲取物件(1.反射獲取,2 bean id獲取)
- getBean()默認創建物件為單例
3.DI注入
1.準備一個物體類
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> map;
private Set<String> set;
private String wife;
private Properties info;
}
2.xml組態檔實作Dl注入
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--p標簽 p:address="12" property
c:address="" 構造器 -->
<bean id="address" p:address="12" >
</bean>
<!--autowire自動裝配 -->
<bean id="student" >
<!-- 普通的set注入-->
<property name="name" value="https://www.cnblogs.com/sky1023/p/普通的set注入"/>
<!--參考bean注入 ref-->
<property name="address" ref="address"/>
<!--陣列-->
<property name="books" >
<array value-type="java.lang.String">
<value>book1</value>
<value>book2</value>
</array>
</property>
<!--list集合-->
<property name="hobbys">
<list>
<value>hobby1</value>
<value>hobby2</value>
</list>
</property>
<!-- map集合-->
<property name="map" >
<map>
<entry key="map" value="https://www.cnblogs.com/sky1023/p/Spring"/>
<entry key="map1" value="https://www.cnblogs.com/sky1023/p/SpringIOC"/>
</map>
</property>
<!-- set集合-->
<property name="set">
<set>
<value>COC</value>
</set>
</property>
<!-- null-->
<property name="wife">
<null></null>
</property>
<!--propreties-->
<property name="info">
<props>
<prop key="name">root</prop>
</props>
</property>
</bean>
</beans>
4.Spring的注解
1.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--開啟注解-->
<context:annotation-config/>
<!-- com.*或者com..*都可以 掃描com包下面的 -->
<context:component-scan base-package="com.*"></context:component-scan>
</beans>
2.注解的使用
注解說明-
- @Service用于Service層
- @repository 用于dao層-
- @controller用于action層
- @component其他類
本質上沒有區別,只是為了區分
@Component
@Scope(value = "https://www.cnblogs.com/sky1023/p/singleton")
public class People {
@Autowired
@Qualifier(value = "https://www.cnblogs.com/sky1023/p/knife")
private arms a;
private String name;
@Override
public String toString() {
return "People{" +
"a=" + a +
", name='" + name + '\'' +
'}';
}
}
- @Autowired(required = false) 通過byType尋找
- @Qualifier(value="https://www.cnblogs.com/sky1023/p/gun") //選擇哪個Value值為對應的值
- @Nullable 注解后,欄位可以為null
- @Scope 作用域
- @value 賦值(簡單賦值)
3.junit測驗
@Test
public void Test1(){
ApplicationContext ApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
People bean = ApplicationContext.getBean(People.class);
System.out.println(bean);
}
運行結果:
People{a=knife{type='刀'}, name='null'}
注解開發先對于xml開發要方便快速的很多,
4.純注解Spring
- 完全使用注解開發
1.java組態檔
@Configuration跟applicationContext.xml配置擁有同等效益
@bean相當于xml的bean標簽
@Configuration
@ComponentScan("com.*")//掃描 默認掃描
public class appConfig {
@Bean
public User user(){
return new User();
}
@Bean
public People people(){
return new People();
}
}
5.總結
- 在xml配置適用于資料庫配置
- 注解配置 用于不初始值配置
- @Bean 用于初始值配置
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/167438.html
標籤:Java
