注入方式 | 配置方式 |
通过Bean的set方法注入 | |
通过构造Bean的方法进行注入 | |
其中,ref是reference的缩写形式,翻译为:涉及,参考的意思,用于引用其它Bean的id,value用于指定属性值
注入数据类型
普通数据类型:String、int、boolean,通过value属性指定
引用数据类型:UserDAOImpl,通过ref属性指定
集合数据类型:List、Map、Properties等
普通数据类型的集合
配置文件
- <bean name="UserService" class="com.example.Service.Impl.UserServiceImpl">
- <property name="stringList">
- <list>
- <value>aaaavalue>
- <value>bbbbvalue>
- <value>ccccvalue>
- list>
- property>
- bean>
引用数据类型的集合
配置文件(两种方式都可以)
- <property name="DAOList">
- <list>
- <ref bean="DAO1">ref>
- <ref bean="DAO2">ref>
- <ref bean="DAO3">ref>
- <ref bean="DAO4">ref>
- list>
- property>
- bean>
- <bean name="DAO1" class="com.example.DAO.Impl.UserDAOImpl">bean>
- <bean name="DAO2" class="com.example.DAO.Impl.UserDAOImpl">bean>
- <bean name="DAO3" class="com.example.DAO.Impl.UserDAOImpl">bean>
- <bean name="DAO4" class="com.example.DAO.Impl.UserDAOImpl">bean>
- <property name="DAOList">
- <list>
- <bean name="DAO1" class="com.example.DAO.Impl.UserDAOImpl">bean>
- <bean name="DAO2" class="com.example.DAO.Impl.UserDAOImpl">bean>
- <bean name="DAO3" class="com.example.DAO.Impl.UserDAOImpl">bean>
- <bean name="DAO4" class="com.example.DAO.Impl.UserDAOImpl">bean>
- list>
- property>
bean的对应类
- package com.example.Service.Impl;
-
- import com.example.DAO.UserDAO;
- import com.example.Service.UserService;
-
- import java.util.List;
-
-
- public class UserServiceImpl implements UserService {
- private List
stringList; - private List
DAOList; -
- public void setStringList(List stringList) {
- this.stringList = stringList;
- }
-
- public void setDAOList(List daoList) {
- this.DAOList = daoList;
- }
-
- public void showStringList() {
- System.out.println(stringList);
- System.out.println(DAOList);
- }
-
-
- }
测试代码
- package com.example.Test;
-
- import com.example.Service.Impl.UserServiceImpl;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class TestApplicationContext {
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
- UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserService");
- userServiceImpl.showStringList();
- }
- }
运行结果
以上介绍的是List集合
下列直接展示Set、Map、Properties
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
- <bean name="UserService" class="com.example.Service.Impl.UserServiceImpl">
- <property name="stringList">
- <list>
- <value>aaaavalue>
- <value>bbbbvalue>
- <value>ccccvalue>
- list>
- property>
- <property name="DAOList">
- <list>
- <ref bean="DAO1">ref>
- <ref bean="DAO2">ref>
- <ref bean="DAO3">ref>
- <ref bean="DAO4">ref>
- list>
- property>
- <property name="StringSet">
- <set>
- <value>aaaavalue>
- <value>bbbbvalue>
- <value>ccccvalue>
- set>
- property>
- <property name="DAOSet">
- <set>
- <ref bean="DAO1"/>
- <ref bean="DAO2"/>
- <ref bean="DAO3"/>
- <ref bean="DAO4"/>
- set>
- property>
- <property name="StringMap">
- <map>
- <entry key="StringMap1" value="aaaa">entry>
- <entry key="StringMap2" value="bbbb">entry>
- <entry key="StringMap3" value="cccc">entry>
- map>
- property>
- <property name="DAOMap">
- <map>
- <entry key="DAOMap1" value-ref="DAO1">entry>
- <entry key="DAOMap1" value-ref="DAO2">entry>
- <entry key="DAOMap1" value-ref="DAO3">entry>
- <entry key="DAOMap1" value-ref="DAO4">entry>
- map>
- property>
-
- bean>
- <bean name="DAO1" class="com.example.DAO.Impl.UserDAOImpl">bean>
- <bean name="DAO2" class="com.example.DAO.Impl.UserDAOImpl">bean>
- <bean name="DAO3" class="com.example.DAO.Impl.UserDAOImpl">bean>
- <bean name="DAO4" class="com.example.DAO.Impl.UserDAOImpl">bean>
- beans>
Bean对应的类
- package com.example.Service.Impl;
-
- import com.example.DAO.UserDAO;
- import com.example.Service.UserService;
-
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
-
-
- public class UserServiceImpl implements UserService {
- // todo 无参构造方法
- // public UserServiceImpl() {
- // System.out.println("无参构造方法");
- // }
-
- // todo 有参构造方法
- // public UserServiceImpl(String name) {
- // System.out.println("有参构造方法");
- // System.out.println(name);
- // }
-
- // private UserDAO userDAO;
- //
- // public void setUserDAO(UserDAO userDAO) {
- // }
- private List
stringList; - private List
DAOList; - private Set
StringSet; - private Set
DAOSet; - private Map
StringMap; - private Map
DAOMap; -
- public void setStringList(List stringList) {
- this.stringList = stringList;
- }
-
- public void setStringSet(Set
stringSet) { - this.StringSet = stringSet;
- }
-
- public void setDAOList(List daoList) {
- this.DAOList = daoList;
- }
-
- public void show() {
- System.out.println("StringList:" + stringList);
- System.out.println("DAOList:" + DAOList);
- System.out.println("StringSet:" + StringSet);
- System.out.println("DAOSet:" + DAOSet);
- System.out.println("StringMap:" + StringMap);
- System.out.println("DAOMap:" + DAOMap);
- }
-
-
- public void setDAOSet(Set daoSet) {
- this.DAOSet = daoSet;
- }
-
- public void setStringMap(Map stringMap) {
- this.StringMap = stringMap;
- }
-
- public void setDAOMap(Map daoMap) {
- this.DAOMap = daoMap;
- }
- }
测试类
- package com.example.Test;
-
- import com.example.Service.Impl.UserServiceImpl;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class TestApplicationContext {
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
- UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserService");
- userServiceImpl.show();
- }
- }
运行结果
上述Bean的值都需要自己手动注入,下列展示自动注入
<bean id="userService" class="com.example.Service.Impl.UserServiceImpl" autowire="byType"/>
配置文件
- <bean id="userService" class="com.example.Service.Impl.UserServiceImpl" autowire="byName"/>
- <bean id="userDAO" class="com.example.DAO.Impl.UserDAOImpl">bean>
Bean对应的类
在该类总注入UserDAO(代码中体现在SetXxx()方法中)
- package com.example.Service.Impl;
-
- import com.example.DAO.UserDAO;
- import com.example.Service.UserService;
-
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
-
-
- public class UserServiceImpl implements UserService {
- private UserDAO userDAO;
-
- public void setUserDAO(UserDAO userDAO) {
- this.userDAO = userDAO;
- }
-
- public void show() {
- System.out.println(userDAO);
- }
-
- }
测试类代码
- package com.example.Test;
-
- import com.example.Service.Impl.UserServiceImpl;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class TestApplicationContext {
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
- UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userService");
- userServiceImpl.show();
- Object userDAO = context.getBean("userDAO");
- System.out.println(userDAO);
- }
- }
运行结果
- <bean id="userService" class="com.example.Service.Impl.UserServiceImpl">
- <property name="userDAO">
- <ref bean="userDAO">ref>
- property>
- bean>
- <bean id="userDAO" class="com.example.DAO.Impl.UserDAOImpl">bean>
-
"userService" class="com.example.Service.Impl.UserServiceImpl" autowire="byType"> -
-
"userDAO" class="com.example.DAO.Impl.UserDAOImpl">