关于java:Spring-XML-注入

11次阅读

共计 7892 个字符,预计需要花费 20 分钟才能阅读完成。

Spring XML 注入

依赖注入:

从 Spring 容器中取出对象,注入到须要的中央。

依据所须要理解的注入形式创立下方的类。

创立 UserServiceImpl 类

package com.arno.service.impl;
​
import com.arno.dao.UserDao;
import com.arno.entity.User;
import com.arno.service.UserService;
​
public class UserServiceImpl implements UserService {
 private UserDao userDao;
 public void setUserDao(UserDao userDao){this.userDao=userDao;}
 @Override
 public boolean addUser(User user) {System.out.println("UserSerivceImpl.addUser()");
 userDao.addUser(user);
 return false;
 }
​
 @Override
 public boolean updateUser(User user) {System.out.println("UserSerivceImpl.updateUser()");
 userDao.updateUser(user);
 return false;
 }
​
}
​

创立 UserDaoImpl 类

package com.arno.dao.impl;
​
import com.arno.dao.UserDao;
import com.arno.entity.User;
​
public class UserDaoImpl implements UserDao {
 private String driverClass;
 private int port;
 private String dbUserName;
 private String dbPassword;
 
 public void setDriverClass(String driverClass) {this.driverClass = driverClass;}
​
 public void setPort(int port) {this.port = port;}
​
 public void setDbUserName(String dbUserName) {this.dbUserName = dbUserName;}
​
 public void setDbPassword(String dbPassword) {this.dbPassword = dbPassword;}
​
 @Override
 public int addUser(User user) {System.out.println("UserDaoImpl.adduser()");
 System.out.println(driverClass+""+port+"   "+dbUserName+"   "+dbPassword);
 return 0;
 }
​
 @Override
 public int updateUser(User user) {System.out.println("UserDaoImpl.updateuser()");
 return 0;
 }
​
}

创立 Message 类

package com.arno.collection;
​
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
​
public class Message {
 private List list;
 private Set set;
 private Map map;
 private Properties props;
 public List getList() {return list;}
 public void setList(List list) {this.list = list;}
 public Set getSet() {return set;}
 public void setSet(Set set) {this.set = set;}
 public Map getMap() {return map;}
 public void setMap(Map map) {this.map = map;}
 public Properties getProps() {return props;}
 public void setProps(Properties props) {this.props = props;}
 @Override
 public String toString() {return "Message [list=" + list + ", set=" + set + ", map=" + map + ", props=" + props + "]";
 } 
}

注入数据的形式:

setter 形式注入:

1. 单值注入

在 xml 中创立 UserDaoImpl 对象,并且注入 4 个属性值

<!-- 告知 spring 创立 UserDaoImpl 对象        对象名:userDao
 此对象中注入 4 个数据
 property 节点对应一个 set 办法
 name="driverClass"  driverClass->DriverClass->setDriverClass
 setDriverClass 字符串去 UserDaoImpl 类中匹配 setDriverClass 的办法
 value="com.mysql.jdbc.Driver"
 -->
 <bean id="userDao"  class="com.arno.dao.impl.UserDaoImpl">
 <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
 <property name="port" value="3306"></property>
 <property name="dbUserName" value="root"></property>
 <property name="dbPassword" value="root"></property>
 </bean>
2. 对象注入

在 xml 中创立 UserDaoImpl 对象,注入 4 个属性值,对象命名为 userDao,注入下方名为 userService 的对象中

 <!-- 告知 spring 创立 UserDaoImpl 对象        对象名:userDao
 -->
 <bean id="userDao"  class="com.arno.dao.impl.UserDaoImpl">
 <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
 <property name="port" value="3306"></property>
 <property name="dbUserName" value="root"></property>
 <property name="dbPassword" value="root"></property>
 </bean>
 <!-- 告知 spring 创立 UserServiceImpl 对象  对象名:userService
 userService 对象中须要 UserDaoImpl 类的对象
 或
 userService 对象中须要 UserDao 接口的对象
 proerty 属性节点, 每一个 property 节点都会对应一个 setter 办法
 name="userDao" userDao->UserDao->setUserDao
 拿 setUserDao 这个字符串作为办法的名称去 UserServiceImpl 类中寻找是否有 setUserDao 的办法
 如果有就反射调用 setUserDao 办法, 办法的参数就是 ref="userDao"
 ref="userDao"  ref:reference 援用    userDao 是 spring 容器中的一个对象名称
 -->
 <bean id="userService" class="com.arno.service.impl.UserServiceImpl">
 <property name="userDao" ref="userDao"></property>
 </bean>
3. 汇合注入(间接注入和间接注入)

间接注入, 创立 Message 对象,通过标签间接注入属性值

 <!-- 间接汇合注入 -->
 <bean id="message1" class="com.arno.collection.Message">
 <property name="list">
 <list>
 <value> 北京 </value>
 <value> 上海 </value>
 <value> 广州 </value>
 <ref bean="hello"></ref>
 </list>
 </property>
 <property name="set">
 <set>
 <value> 北京 </value>
 <value> 上海 </value>
 <value> 广州 </value>
 <ref bean="hello"></ref>
 </set>
 </property>
 <property name="map">
 <map>
 <entry key="bj" value="北京"></entry>
 <entry key="sh" value="上海"></entry>
 <entry key="gz" value="广州"></entry>
 </map>
 </property>
 <property name="props">
 <props>
 <prop key="bj"> 北京 </prop>
 <prop key="sh"> 上海 </prop>
 <prop key="gz"> 广州 </prop>
 </props>
 </property>
 </bean>

间接注入, 创立 Hello 对象和汇合。创立 Message 对象,通过标签援用 hello 对象和汇合注入到 Message 对象的属性中

 <!-- 间接汇合注入 -->
 <bean id="hello" class="com.arno.ioc.Hello"></bean>
 <util:list id="ulist">
 <value> 北京 </value>
 <value> 上海 </value>
 <value> 广州 </value>
 <ref bean="hello"></ref>
 </util:list>
 <util:set id="uset">
 <value> 北京 </value>
 <value> 上海 </value>
 <value> 广州 </value>
 <ref bean="hello"></ref>
 </util:set>
 <util:map id="umap">
 <entry key="bj" value="北京"></entry>
 <entry key="sh" value="上海"></entry>
 <entry key="gz" value="广州"></entry>
 <entry key="hello" value-ref="hello"></entry>
 </util:map>
 <util:properties id="uprops">
 <prop key="bj"> 北京 </prop>
 <prop key="sh"> 上海 </prop>
 <prop key="gz"> 广州 </prop>
 </util:properties>
 <bean id="message2" class="com.arno.collection.Message">
 <property name="list" ref="ulist"></property>
 <property name="set" ref="uset"></property>
 <property name="map" ref="umap"></property>
 <property name="props" ref="uprops"></property>
 </bean>
4. 属性注入(把属性文件中的数据注入给对象中)

${} 形式:创立 mysql.properties 文件 ( 留神躲避关键词)

 jdbc_driverClass=com.mysql.jdbc.Driver
 jdbc_url=jdbc:mysql://localhost:3306/tesdb
 jdbc_userName=root
 jdbc_userPassword=root

创立 jdbcUtil1 和 jdbcUtil2 类

public class JDBCUtil1 {
 private String driverClass;
 private String url;
 private String userName;
 private String userPassword;
 getter and setter(生成 get/set 办法)
 }

通过 ${} 形式获得 mysql.properties 内容并注入到 jdbcUtil1 对象中

 <!-- 用 spring 把属性文件的内容加载到 spring 容器中 , 多个属性文件用逗号距离 -->
 <context:property-placeholder location="classpath:conf/mysql.properties"/>
 
 <bean id="jdbcUtil1" class="com.arno.properties.JDBCUtil1">
 <property name="driverClass" value="${jdbc_driverClass}"></property>
 <property name="url" value="${jdbc_url}"></property>
 <property name="userName" value="${jdbc_userName}"></property>
 <property name="userPassword" value="${jdbc_userPassword}"></property>
 </bean>

{} 形式:通过 #{} 形式获得 mysql.properties 内容并注入到 jdbcUtil1 对象中

 <!-- 用 spring 把属性文件的内容加载到 spring 容器中 , 多个属性文件用逗号距离 -->
 <util:properties id="manyProperties"
 location="classpath:conf/mysql.properties"></util:properties>
 
 <bean id="jdbcUtil2" class="com.arno.properties.JDBCUtil2">
 <property name="driverClass" value="#{manyProperties.jdbc_driverClass}"></property>
 <property name="url" value="#{manyProperties.jdbc_url}"></property>
 <property name="userName" value="#{manyProperties.jdbc_userName}"></property>
 <property name="userPassword" value="#{manyProperties.jdbc_userPassword}"></property>
 </bean>
5. 空值注入

空值两种:”” 空字符串和 null 空指针

创立 Kong 类

package com.arno.kong;
​
public class Kong {
 private String str1;
 private String str2;
 public String getStr1() {return str1;}
 public void setStr1(String str1) {this.str1 = str1;}
 public String getStr2() {return str2;}
 public void setStr2(String str2) {this.str2 = str2;}
 
}

创立 Kong 对象,注入“”和 null

 <!--str1 存储的是空字符串 ,str2 存储的 null-->
 <bean id="kong" class="com.arno.kong.Kong">
 <property name="str1" value=""></property>
 <property name="str2">
 <null />
 </property>
 </bean>

下面的五种形式用 setter 形式把数据注入到须要的对象中

结构形式注入:

用构造方法注入数据: 创立 Hello 对象和 ConstructorClass 对象,并通过 ConstructorClass 构造方法注入 Hello 对象和属性值

 <bean id="hello" class="com.arno.ioc.Hello"></bean>
 <bean id="constructorClass" class="com.arno.constructor.ConstructorClass">
 <constructor-arg index="0" value="张三"></constructor-arg>
 <constructor-arg index="1" value="20"></constructor-arg>
 <constructor-arg index="2" ref="hello"></constructor-arg>
 
 </bean>

创立 ConstructorClass 类,并创立构造方法

ConstructorClass.java
 public class ConstructorClass {
 private String name;
 private String  age;
 private Hello hello;
 
 public ConstructorClass(String name, String age, Hello hello) {
 this.name = name;
 this.age = age;
 this.hello = hello;
 }
 public String getName() {return name;}
 public String getAge() {return age;}
 public Hello getHello() {return hello;}
 
 }

总结: 在开发中用 setter 办法是最多, 也最罕用, 灵便也最好

在开发中结构注入不罕用, 结构注入数据个别用在框架中居多

autowire 主动拆卸

byType 依据类型注入,创立 User 对象,依据 User 对象中须要注入的属性,在 spring 容器中寻找是否有对应类型的对象, 如果有就注入 前提: 在 spring 容器中对应的类类型不能两个及以上

 <bean id="user" 
 class="com.arno.autowire.User"
 autowire="byType">
 </bean>

byName 依据名字注入,例如 User 中有 Dog 类须要注入,并且 User 类中有 setDog() 办法,依据 setDog 办法名,去掉 set 并首字母小写失去名字 dog,在 spring 容器中寻找是否有对应名字的对象,而后通过 set 办法进行注入。

<!-- 
 autowire="byName"  依照名称主动拆卸
 过程: 首先搜寻 User 类, 类种须要两个对象, 别离是 Cat 类和 Dog 类
 且类中有 setCat 办法,setCat->Cat->cat, 拿 cat 去 spring 容器中寻找是否有此对象
 -->
 <bean id="user" 
 class="com.arno.autowire.User"
 autowire="byName">
 </bean>
正文完
 0