bean实例化的三种方式
- 无参构造方法实例化
- 工厂静态方法实例化
- 工厂实例方法实例化
工厂静态方法化
package cc.mllt.dao.factory; import cc.mllt.dao.UserDao; import cc.mllt.dao.impl.UserDaoImpl; public class StaticFactory { public static UserDao getUserDao(){ return new UserDaoImpl(); } }
发现包放的有问题,更改一下路径
更改配置文件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.xsd"> <!-- 无参构造--> <!-- <bean id="userDao" class="cc.mllt.dao.impl.UserDaoImpl" scope="singleton" init-method="ml_init" destroy-method="ml_destroy"/>--> <!-- 静态工厂构造--> <bean id="userDao" class="cc.mllt.factory.StaticFactory" factory-method="getUserDao" /> </beans>
工厂实例方法实例化
package cc.mllt.factory; import cc.mllt.dao.UserDao; import cc.mllt.dao.impl.UserDaoImpl; public class DynamicFactory { public UserDao getUserDao(){ return new UserDaoImpl(); } }
配置文件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.xsd"> <!-- 无参构造--> <!-- <bean id="userDao" class="cc.mllt.dao.impl.UserDaoImpl" scope="singleton" init-method="ml_init" destroy-method="ml_destroy"/>--> <!-- 静态工厂构造--> <!-- <bean id="userDao" class="cc.mllt.factory.StaticFactory" factory-method="getUserDao" />--> <!-- 工厂实例实例化(动态工厂)--> <bean id="factory" class="cc.mllt.factory.DynamicFactory"></bean> <bean id="userDao" factory-bean="factory" factory-method="getUserDao"></bean> </beans>
测试
Spring配置文件
Bean的依赖注入分析
package cc.mllt.service.ipml; import cc.mllt.dao.UserDao; import cc.mllt.service.UserService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserServiceImpl implements UserService { @Override public void save() { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = (UserDao) app.getBean("userDao"); userDao.save(); } }
package cc.mllt.demo; import cc.mllt.service.UserService; import cc.mllt.service.ipml.UserServiceImpl; public class UserController { public static void main(String[] args) { UserService userService = new UserServiceImpl(); userService.save(); } }
现在
这是由于还没有去修改配置未见ApplicationContext.xml
UserDao此时在UserService内部
Bean依赖注入概念
依赖注入(Dependency Injection):Spring框架核心IOC的具体实现
IOC解耦只是降低了他们的依赖关系,但不会消除。例如:业务层仍然会调用持久层的方法
Bean的依赖注入方式
set方法
p命名空间注入的本质也是set方法注入,但是比起上述的set方法注入更加方便,主要体现在配置文件中
(1)引入p命名空间:
xmlns:p="http://www.springframework.org/schema/p"
(2)修改注入方法
<bean id="userService" class="cc.mllt.service.ipml.UserServiceImpl" p:userDao-ref="userDao"></bean>
构造方法
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 无参构造--> <!-- <bean id="userDao" class="cc.mllt.dao.impl.UserDaoImpl" scope="singleton" init-method="ml_init" destroy-method="ml_destroy"/>--> <!-- 静态工厂构造--> <!-- <bean id="userDao" class="cc.mllt.factory.StaticFactory" factory-method="getUserDao" />--> <!-- 工厂实例实例化(动态工厂)--> <!-- <bean id="factory" class="cc.mllt.factory.DynamicFactory"></bean>--> <!-- <bean id="userDao" factory-bean="factory" factory-method="getUserDao"></bean>--> <!-- service--> <!-- set构造方法--> <!-- <bean id="userDao" class="cc.mllt.dao.impl.UserDaoImpl"></bean>--> <!-- <bean id="userService" class="cc.mllt.service.ipml.UserServiceImpl">--> <!-- <property name="userDao" ref="userDao"></property>--> <!-- </bean>--> <!-- <bean id="userService" class="cc.mllt.service.ipml.UserServiceImpl" p:userDao-ref="userDao"></bean>--> <!--构造方法--> <bean id="userDao" class="cc.mllt.dao.impl.UserDaoImpl"></bean> <bean id="userService" class="cc.mllt.service.ipml.UserServiceImpl"> <constructor-arg name="userDao" ref="userDao"></constructor-arg> </bean> </beans>
package cc.mllt.service.ipml; import cc.mllt.dao.UserDao; import cc.mllt.service.UserService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserServiceImpl implements UserService { private UserDao userDao; public UserServiceImpl(UserDao userDao){ this.userDao = userDao; } public UserServiceImpl(){ } // private UserDao userDao; // public void setUserDao(UserDao userDao){ // this.userDao = userDao; // } @Override public void save() { // ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); // UserDao userDao = (UserDao) app.getBean("userDao"); userDao.save(); } }
运行