简介
在上一篇博客中我们从Spring零基础说到了通过配置文件让Spring容器管理JavaBean和JavaBean通过配置文件注入。
下面我们将说到Spring通过注释管理bean对象。通过注释注入对象等。
Spring的bean管理(注解)
注解介绍
- 代码里面特殊标记,使用注解可以完成功能
- 注解写法 @注解名称(属性名称=属性值)
- 注解使用在类上面,方法上面 和 属性上面
Spring注解开发准备
注意:使用注解后并不能完全脱离配置文件,只是在配置文件里面不需要写这么多了而已。
导入jar包:
- 导入基本jar包,就是上篇博客中用到的jar包
- 导入aop的jar包
创建类,创建方法:
实践目标:通过注解创建对象
@Component(value="userInit")//这里我已经提前把注解写好了
public class User {
public void showHello(){
System.out.println("Hello Spring Component!!!");
}
}
创建Spring配置文件,引入约束
- 上篇博客做了Spring的IOC中bean管理的基本功能,引入了约束beans
- 这次做Spring注解开发,引入新的约束,context约束
<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">
整体配置:
<?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">
<!--
开启注解扫描 :
- 到包里面扫描类、方法、属性上面是否有注解
- cn.domarvel这个配置的意思是能够扫描cn.domarvel.entity、cn.domarvel.service等等cn.domarvel包下面的。
-->
<context:component-scan base-package="cn.domarvel"></context:component-scan>
<!--
配置下面这个而不配置上面这个就只会扫描属性上面的注解
-->
<!-- <context:annotation-config base-package="cn.domarvel"></context:annotation-config> -->
</beans>
测试代码:
public class UserTest {
@Test
public void showUserTest(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
User user=(User)context.getBean("userInit");
System.out.println(user);
}
}
//到了这里通过注解创建对象就成功了。
Spring的bean管理中常用注解
@Component:组件(作用在类上)
Spring中提供@Component的三个衍生注解:(功能目前来讲是一致的)
- @Controller———-:WEB层
- @Service————-:业务层
- @Repository———-:持久层
这三个注解是为了让标注类本身的用途清晰,Spring在后续版本会对其增强。
通过注解实现javabean创建对象是单实例还是多实例
在类名上边配置:@Scope(value="prototype"),默认不写Scope为单实例。属性名字是value时,属性名称可以省略。如@Scope(“prototype”)
singleton:单例
prototype:多例
@Component(value="userInit")
@Scope(value="prototype")
public class User {
public void showHello(){
System.out.println("Hello Spring Component!!!");
}
}
注解注入属性
注解两种写法:
- @Autowired
- @Resource(name=”“),属性值写其它类创建对象的注释值。如我们创建在UserDao类里面配置@Component(“userDao”),那么在其它类注入该属性就这样写:@Resource(name=”userDao”),记住这里一定要写对
- 一般我们用的是Resource注释注入属性
创建Service类,创建Dao类,在Service类里面得到dao对象
根据类名自动去找到对应的类帮你注入属性到方法
创建dao对象并且写上注解:
@Component("userDao")//这里可以省略了value属性名,如果不省略就是@Component(value="userDao")
public class UserDao {
public void showDao(){
System.out.println("Hello Dao!!!");
}
}
创建service对象并且写上注解:
@Service("userService")
public class UserService {
//得到dao对象:
//定义dao类型属性
//使用注解方式时不需要set方法,强调:两种注入属性方式都不需要set方法。
//通过Spring配置文件方式创建对象需要set方法。
//在dao属性上面使用注解,完成对象注入
@Autowired
//注意Autowired查找类的方式是通过类名查找的,再次强调,是类名,类名。和它@Component("userDao")无关,它可以随便写。变量名private UserDao userDao;也可以随便写!!!
private UserDao userDao;
public void showService(){
System.out.println("show UserService!!!");
userDao.showDao();
}
}
测试类:
@Test
public void showUserService(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService=(UserService)context.getBean("userService");
userService.showService();
}
配置文件和注解混合使用
1.创建对象操作使用配置文件方式实现
2.注入属性的操作使用注解方式实现
创建类:
public class BookDao {
public void showBook(){
System.out.println("Book Dao!!!");
}
}
public class BookService {
@Resource(name="bookDao")
private BookDao bookDao;
public void showBookService(){
System.out.println("show BookService!!");
bookDao.showBook();
}
}
<!-- 配置文件 -->
<?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">
<context:component-scan base-package="cn.domarvel"></context:component-scan>
<bean id="bookDao" class="cn.domarvel.dao.BookDao"></bean>
<bean id="bookService" class="cn.domarvel.service.BookService"></bean>
</beans>
//测试类:
public class BookTest {
@Test
public void showBookInject(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
BookService bookService=(BookService)context.getBean("bookService");
bookService.showBookService();
}
}
//运行结果:
/*
show BookService!!
Book Dao!!!
*/
本文详细介绍了Spring框架中使用注解进行依赖注入的方法,包括如何使用@Component及其派生注解来声明组件,如何通过@Autowired和@Resource注解进行属性注入,以及如何结合XML配置文件进行混合配置。

被折叠的 条评论
为什么被折叠?



