Spring的bean管理(注解注入属性和注解文件混合使用)
注解方式注入属性
1 创建service类,创建dao类,在service得到dao对象
注入属性第一个注解 @Autowired
(1) 创建dao和service对象
(2) 在service类里面定义到类型属性
注入属性第一个注解 @Autowired
![](https://img-blog.csdn.net/20170530192701746?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemh1cGVuZ3Fx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
注入属性第二个注解@Resource
注入属性第一个注解 @Autowired
注入属性第二个注解@Resource
具体代码实例
第一步加注解扫描:bean1.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"
xmlns:context="http://www.springframework.org/schema/context" 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">
<!-- 开启注解扫描 (1) 到包里面扫描类 方法 属性上面是否有注解 -->
<context:component-scan base-package="com.cn.anno"></context:component-scan>
</beans>
UsersDao.java
package com.cn.anno;
import org.springframework.stereotype.Service;
@Service(value="userDao")
public class UsersDao {
public void add(){
System.out.println("dao..........");
}
}
UserServlce.javapackage com.cn.anno;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component(value="userService")
public class UserServlce {
//得到dao对象
//定义dao类型属性
//在dao属性上面使用注解 完成对象注入
// @Autowired
// private UsersDao userDao;
//使用注解方式时候,不需要set方法
//name属性值写注解创建dao对象
@Resource(name="userDao")
private UsersDao userDao;
public void add(){
System.out.println("servlice........");
userDao.add();
}
}
package com.cn.anno;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAnno {
@Test
public void testService(){
ApplicationContext context=
new ClassPathXmlApplicationContext("bean1.xml");
UserServlce user=(UserServlce) context.getBean("userService");
user.add();
}
}
测试结果
下面讲(注解和配置文件混合使用)
配置文件和注解混合使用
1.创建对象操作使用配置文件方式实现
![](https://img-blog.csdn.net/20170530193935199?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemh1cGVuZ3Fx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
2 注入属性的操作使用注解方式实现
1.创建对象操作使用配置文件方式实现
2 注入属性的操作使用注解方式实现
具体代码实现
配置文件 bean2.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"
xmlns:context="http://www.springframework.org/schema/context" 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">
<!-- 开启注解扫描 (1) 到包里面扫描类 方法 属性上面是否有注解 -->
<context:component-scan base-package="com.cn.anno"></context:component-scan>
<!-- 配置对象 -->
<bean id="bookService" class="com.cn.xmlanno.BookService"></bean>
<bean id="bookDao" class="com.cn.xmlanno.BookDao"></bean>
<bean id="orderDao" class="com.cn.xmlanno.OrderDao"></bean>
</beans>
BookDao.java
package com.cn.xmlanno;
public class BookDao {
public void book(){
System.out.println("booksdao......");
}
}
OrderDao.java
package com.cn.xmlanno;
public class OrderDao {
public void buy(){
System.out.println("ordersdao......");
}
}
BookService.java
package com.cn.xmlanno;
import javax.annotation.Resource;
public class BookService {
//得到bookdao和ordersdao对象
@Resource(name="bookDao")
private BookDao bookDao;
@Resource(name="orderDao")
private OrderDao orderDao;
public void add(){
System.out.println("service......");
bookDao.book();
orderDao.buy();
}
}
测试类TestAnno.java
package com.cn.xmlanno;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAnno {
@Test
public void testService(){
ApplicationContext context=
new ClassPathXmlApplicationContext("bean2.xml");
BookService book=(BookService) context.getBean("bookService");
book.add();
}
}
测试结果