对象依赖关系:DI 依赖注入
1. 通过构造方法
2. 通过 set方法对属性注入【常用】
3. p名称空间,属性注入优化
4. 自动装配(了解)(不推荐使用:因为Spring提供的自动装配为了简化配置,但是不利于后期维护;)
5. 注解方式(网址: https://blog.csdn.net/qq_41029923/article/details/84103960 )
【 3. p名称空间进行属性注入,将属性注入优化(① xml文件中文件头中要有 xmlns:p="http://www.springframework.org/schema/p" 这句; ② 之前属性注入:在< bean>中用< property>用 name="id" 、value="101" 属性; p名称空间< bean>中用 p:id="101" p:name="Jack" ;③ p名称空间引用已经存在的 bean,< bean>中用 p:x-ref="x",如:userDao对象已存在,userService中需要注入 userDao对象属性,用 p:userDao-ref="userDao";)
4. 自动装配:(a. 根据名称字段装配:autowire="byName",自动IOC容器中找与属性同名的对象(即属性名 set方法和 bean中对象的 id要完全相同),每个< bean>中添加 autowire="byName",或者全局添加即< beans>中添加 default-autowire="byName";
b. 根据类型装配:autowire="byType",IOC容器中只能有一个同类型对象,否则报错;也可在< beans>中添加 default-autowire="byType"进行全局定义。)
】
1. 通过构造方法
2. 通过 set方法对属性注入
(< bean>中< property>标签中利用 name 和 value 属性:name属性是用 set方法,去掉 set再首字母小写,value是为 name的 set方法对应的属性赋值; < bean>中< property>标签中利用 name 和 ref 属性:name属性是用 set方法,去掉 set再首字母小写,ref 引用已生成的 bean对象; 内部 bean:bean在内部,不需要 id,只能给外层的 bean使用,在外部不能使用了。)
1.1 User.java
public class User{
private int id;
private String name;
set、get();
public User(){
System.out.println("----对象被创建----");
System.out.println("----无参构造----");
}
public User(int id,String name){
System.out.println("----对象被创建----");
System.out.println("----有参构造----");
this.id=id; //
this.name=name; //
}
public void init_user(){
System.out.println("------对象初始化-----")
}
public void destory_user(){
System.out.println("------对象销毁-----")
}
}
1.2 bean.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 依赖注入 -->
<!-- 1. 通过构造方法对属性注入 -->
<bean id="user1" class="com.asd.User">
<constructor-arg value="tom" index="1" type="java.lang.String"></constructor-arg>
<constructor-arg value="101" index="0" type="int"></constructor-arg>
</bean>
<bean id="s" class="java.lang.String">
<constructor-arg value="tom"></constructor-arg>
</bean>
<bean id="user2" class="com.asd.User">
<constructor-arg index="1" ref="s"></constructor-arg>
<constructor-arg value="101" index="0" type="int"></constructor-arg>
</bean>
<!-- 2. 通过set方法对属性注入 -->
<bean id="user3" class="com.asd.User">
<property name="id" value="105"></property>
<property name="name" value="tom"></property>
</bean>
</beans>
1.3 Test.java
public class Test{
public static void main(String[] args) {
/*得到IOC容器*/
ApplicationContext ApplicationContext=new ClassPatnXmlApplicationContext("bean.xml");
System.out.println("----得到IOC容器----");
/*从容器中获取对象*/
User user=(User)applicationContext.getBean("user3");
System.out.println(user);
}
}
2.1 UserAction.java
public class UserAction{
private UserSerice userService;
public void setUserService(UserService userService){
this.userService=userService;
}
public String addUser(){
userService.save();
return null;
}
}
2.2 UserService.java
public class UserService{
private UserDao userDao;
public void setUserDao(UserDao userDao){
this.userDao=userDao;
}
public void save(){
userDao.save();
}
}
2.3 UserDao.java
public class UserDao{
public void save(){
System.out.println("保存到数据库");
}
}
2.4 bean4.xml
(< bean>中< property>标签中利用 name 和 ref 属性:name属性是用 set方法,去掉 set再首字母小写,ref 引用已生成的 bean对象;)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- Action -->
<bean id="userAction" class="com.asd.UserAction">
<property name="userService" ref="userService"></property>
</bean>
<!-- Service -->
<bean id="userService" class="com.asd.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- Dao -->
<bean id="userDao" class="com.asd.UserDao"></bean>
<!-- 内部bean -->
<bean id="userAction2" class="com.asd.UserAction">
<property name="userService">
<bean class="com.asd.UserService">//bean只能在内部使用,没有id
<property name="userDao">
<bean class="com.asd.UserDao"></bean>
</property>
</bean>
</property>
</bean>
</beans>
2.5 Test.java
public class Test{
public static void main(String[] args) {
/*得到IOC容器*/
ApplicationContext ApplicationContext=new ClassPatnXmlApplicationContext();
/*从容器中获取对象*/
UserAction userAction=(UserAction)applicationContext.getBean("userAction");//userAction2(内部bean)
userAction.addUser();
}
}
运行结果:
3. p名称空间
(① xml文件中文件头中要有 xmlns:p="http://www.springframework.org/schema/p" 这句; ② 之前属性注入:在< bean>中用< property>用 name="id" 、value="101" 属性; p名称空间< bean>中用 p:id="101" p:name="Jack" ;③ p名称空间引用已经存在的 bean,< bean>中用 p:x-ref="x",如:userDao对象已存在,userService中需要注入 userDao对象属性,用 p:userDao-ref="userDao";)
3.4 bean5.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- p 名称空间对属性注入 -->
<bean id="user" class="com.asd.User" p:id="106" p:name="Jack">
//<property name="id" value="108"></property>
</bean>
<bean id="userDao" class="com.asd.UserDao"></bean>
<bean id="userService" class="com.asd.UserService" p:userDao-ref="userDao"></bean>
<bean id="userAction" class="com.asd.UserAction" p:userService-ref="userService"></bean>
</beans>
3.5 Test.java
public class Test{
public static void main(String[] args) {
/*得到IOC容器*/
ApplicationContext ApplicationContext=new ClassPatnXmlApplicationContext("bean5.xml");
/*从容器中获取对象*/
User user=(User) applicationContext.getBean("user");
System.out.println(user);
System.out.println("---------");
UserAction userAction=(UserAction)applicationContext.getBean("userAction");
userAction.addUser();
}
}
Test.java 中直接输出user时运行结果:
Test.java 中添加 userAction时运行结果:
4. 自动装配
(a. 根据名称字段装配:autowire="byName",自动IOC容器中找与属性同名的对象(即属性名 set方法和 bean中对象的 id要完全相同),每个< bean>中添加 autowire="byName",或者全局添加即< beans>中添加 default-autowire="byName";
b. 根据类型装配:autowire="byType",IOC容器中只能有一个同类型对象,否则报错;也可在< beans>中添加 default-autowire="byType"进行全局定义。)
4.4 bean6.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" //default-autowire="byName">
<!-- 自动装配 byName -->
<bean id="userDao" class="com.asd.UserDao"></bean>
<bean id="userService" class="com.asd.UserService" autowire="byName"></bean>
<bean id="userAction" class="com.asd.UserAction" autowire="byName"></bean>
<!-- 自动装配 byType -->
<bean id="userDao" class="com.asd.UserDao"></bean>
//<bean id="userDao" class="com.asd.UserDao"></bean>
<bean id="userService" class="com.asd.UserService" autowire="byType"></bean>
<bean id="userAction" class="com.asd.UserAction" autowire="byType"></bean>
</beans>
4.5 Test.java
public class Test{
public static void main(String[] args) {
/*得到IOC容器*/
ApplicationContext ApplicationContext=new ClassPatnXmlApplicationContext("bean6.xml");
/*从容器中获取对象*/
UserAction userAction=(UserAction)applicationContext.getBean("userAction");
userAction.addUser();
}
}
运行结果: