<properties>
<spring.version>5.0.5.RELEASEspring.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
dependencies>
public interface UserDao {
public void save();
}
public class UserDaoImpl implements UserDao {
@Override
public void save() {
System.out.println("UserDao save method running....");
}
}
<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.xsd">
beans>
<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.xsd">
<bean id="userDao" class="com.destiny.dao.impl.UserDaoImpl">bean>
beans>
@Test
public void test1() {
//加载spring配置文件。获取spring核心容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取spring容器中的userDao对象
UserDao userDao = (UserDao) ac.getBean("userDao");
//调用方法
userDao.save();
}
<bean id="userDao" class="com.destiny.dao.impl.UserDaoImpl">bean>
<bean id="userDao" class="com.destiny.dao.impl.UserDaoImpl" scope="singleton" >bean>
init-method: 指定类中的初始化方法名称,当对象被创建时,调用该方法。
destroy-method: 指定类中销毁方法名称,当对象被销毁时,调用该方法。
1) 使用无参构造方法实例化
<bean id="userDao" class="com.destiny.dao.impl.UserDaoImpl"/>
2) 工厂静态方法实例化
//工厂类
public class StaticFactoryBean {
//静态方法,通过该方法获取对象
public static UserDao createUserDao(){
return new UserDaoImpl();
}
}
<bean id="userDao" class="com.destiny.factory.StaticFactoryBean"
factory-method="createUserDao" />
3) 工厂实例方法实例化
//工厂类
public class DynamicFactoryBean {
//非静态方法,需要先创建工厂类的对象,再通过对象调用该方法,获取对象
public UserDao createUserDao(){
return new UserDaoImpl();
}
}
<bean id="factoryBean" class="com.destiny.factory.DynamicFactoryBean"/>
<bean id="userDao" factory-bean="factoryBean" factory-method="createUserDao"/>
① 在类中提供有参构造
public class UserServiceImpl implements UserService {
private UserDao userDao;
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save() {
userDao.save();
}
}
② 配置Spring容器调用有参构造时进行注入
<bean id="userDao" class="com.destiny.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.destiny.service.impl.UserServiceImpl">
<!-- 使用构造方法,把userDao对象,注入userService中 -->
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
</bean>
① 在UserServiceImpl中添加setUserDao方法
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save() {
userDao.save();
}
}
② 配置Spring容器调用set方法进行注入
<bean id="userDao" class="com.destiny.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.destiny.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
bean>
public class UserDaoImpl implements UserDao {
private String company;
private int age;
public void setCompany(String company) {
this.company = company;
}
public void setAge(int age) {
this.age = age;
}
public void save() {
System.out.println(company + "===" +age);
}
}
<bean id="userDao" class="com.destiny.dao.impl.UserDaoImpl">
<property name="company" value="郑州大学">property>
<property name="age" value="20">property>
bean>
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save() {
userDao.save();
}
}
<bean id="userDao" class="com.destiny.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.destiny.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
bean>
(1)List
public class UserDaoImpl implements UserDao {
private List<String> strList;
public void setStrList(List<String> strList) {
this.strList = strList;
}
public void save() {
System.out.println(strList);
System.out.println("UserDao save method running....");
}
}
<bean id="userDao" class="com.destiny.dao.impl.UserDaoImpl">
<property name="strList">
<list>
<value>aaavalue>
<value>bbbvalue>
<value>cccvalue>
list>
property>
bean>
(2)集合数据类型(List
public class UserDaoImpl implements UserDao {
private List<User> userList;
public void setUserList(List<User> userList) {
this.userList = userList;
}
public void save() {
System.out.println(userList);
System.out.println("UserDao save method running....");
}
}
<bean id="u1" class="com.destiny.domain.User"/>
<bean id="u2" class="com.destiny.domain.User"/>
<bean id="userDao" class="com.destiny.dao.impl.UserDaoImpl">
<property name="userList">
<list>
<bean class="com.itheima.domain.User"/>
<bean class="com.itheima.domain.User"/>
<ref bean="u1"/>
<ref bean="u2"/>
list>
property>
bean>
(3)集合数据类型( Map
public class UserDaoImpl implements UserDao {
private Map<String,User> userMap;
public void setUserMap(Map<String, User> userMap) {
this.userMap = userMap;
}
public void save() {
System.out.println(userMap);
System.out.println("UserDao save method running....");
}
}
<bean id="u1" class="com.destiny.domain.User"/>
<bean id="u2" class="com.destiny.domain.User"/>
<bean id="userDao" class="com.destiny.dao.impl.UserDaoImpl">
<property name="userMap">
<map>
<entry key="user1" value-ref="u1"/>
<entry key="user2" value-ref="u2"/>
map>
property>
bean>
(4)集合数据类型(Properties)的注入
public class UserDaoImpl implements UserDao {
private Properties properties;
public void setProperties(Properties properties) {
this.properties = properties;
}
public void save() {
System.out.println(properties);
System.out.println("UserDao save method running....");
}
}
<bean id="userDao" class="com.destiny.dao.impl.UserDaoImpl">
<property name="properties">
<props>
<prop key="p1">aaaprop>
<prop key="p2">bbbprop>
<prop key="p3">cccprop>
props>
property>
bean>
<import resource="applicationContext-xxx.xml"/>
//加载配置文件,获取Spring核心容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//从Spring核心容器中获取对象
//获取id="userService"的对象
UserService us1 = (UserService)ac.getBean("userService");
//获取类型为"UserService"的对象,
//注意,该操作要求,在spring核心容器中只能有一个"UserService"类型的对象
UserService us2 = ac.getBean(UserService.class);
//获取类型为"UserService",id为"userService"的对象,
UserService us3 = ac.getBean("userService",UserService.class);