DI依賴注入也有兩種方式,即配置文件注入和註解注入java
屬性須知:mysql
前期代碼準備:spring
//Dao.Class文件 public class Dao { private String testDI; public Dao(String testDI){ this.testDI=testDI; } } //Service.Class文件 public class Service { private Dao dao; private String test; private Map<String,String> map; private Properties properties; public void setProperties(Properties properties) { this.properties = properties; } public void setMap(Map<String, String> map) { this.map = map; } public void setDao(Dao dao) { this.dao = dao; } public void setDao(Dao dao) { this.dao = dao; } private String[] args; private List<String> list; public void setArgs(String[] args) { this.args = args; } public void setList(List<String> list) { this.list = list; } }
applicationContext.xml配置文件sql
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> </beans>
配置文件注入又分三種 :數組
使用有參構造注入(以Dao類爲例)app
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="dao" class="com.testWeb.dao.impl.Dao"> <constructor-arg name="testDI" value="測試DI"></constructor-arg> </bean> </beans>
使用set方法注入(以Service類爲例,注意:set方法注入爲經常使用方法,且注入對象也較爲重要,請緊緊掌握)函數
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="dao" class="com.testWeb.dao.impl.Dao"> <constructor-arg name="testDI" value="測試DI"></constructor-arg> </bean> <bean id="service" class="com.testWeb.service.Service"> <property name="dao" ref="dao"></property> </bean> </beans>
P名稱空間注入測試
<?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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="dao" class="com.testWeb.dao.impl.Dao"> <constructor-arg name="testDI" value="測試DI"></constructor-arg> </bean> <bean id="service" class="com.testWeb.service.Service" p:dao-ref="dao" p:test="測試"></bean> </beans>
4.複雜屬性的注入this
這裏複雜屬性的注入其實屬於set注入,但因爲代碼量緣由,就另起一點了。code
<?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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- id:用於SpringIOC調用,能夠爲任意 class:類的全路徑 --> <bean id="user" class="com.testWeb.daomain.User"></bean> <!--開啓註解掃描--> <context:component-scan base-package="com.testWeb"></context:component-scan> <bean id="dao" class="com.testWeb.dao.impl.Dao"> <constructor-arg name="testDI" value="測試DI"></constructor-arg> </bean> <bean id="service" class="com.testWeb.service.Service" p:dao-ref="dao" p:test="測試"> <!--數組--> <property name="args"> <list> <value>測試1</value> <value>測試2</value> <value>測試3</value> </list> </property> <!-- List--> <property name="list"> <list> <value>測試1</value> <value>測試2</value> <value>測試3</value> </list> </property> <!--Map--> <property name="map"> <map> <entry key="name" value="LiMing"></entry> <entry key="class" value="Class1"></entry> <entry key="hoby" value="PingPang"></entry> </map> </property> <!--properties--> <property name="properties"> <props> <prop key="driverclass">com.mysql.jdbc.Driver</prop> </props> </property> </bean> </beans>
第一步、開啓註解包掃描
<?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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--開啓註解掃描--> <context:component-scan base-package="com.testWeb"></context:component-scan> </beans>
第二步、利用註解建立對象並注入屬性
//Dao.class文件 @Service(value = "dao") public class Dao { public void test(){ System.out.println("test"); } } //Service.class文件 @Service(value = "service") public class Service { //獲得dao對象 //在dao屬性上利用註解直接注入,使用註解不用set方法 @Autowired //自動裝配 private Dao dao; //name中註解建立對象的Value值 @Resource(name="dao") private Dao dao1; }