导入spring-aop包才支持注解
修改配置文件:
<?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">
<!--告知spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为
context名称空间和约束中-->
<!--base-package:指定扫描基础包,将基础包以及其下所有的包中加了注解的类,扫描进IOC容器中-->
<context:component-scan base-package="com.jess"></context:component-scan>
</beans>
<context:component-scan base-package="com.jess">
<!--扫描时排除一些不要的组件
1. type:指定排除规则
1.annotation:按注解排除
2.assignable:按类排除
2. expression:排除类型所在的全类名
-->
<context:exclude-filter type="annotation" expression="">
</context:component-scan>
<!--use-default-filters:禁用默认规则-->
<context:component-scan base-package="com.jess" use-default-filters="false">
<!--只扫描哪些组件-->
<context:include-filter type="annotation" expression="">
</context:component-scan>
一. 创建对象
作用与xml中bean标签的作用一致
@Component
:把当前对象存入spring中
属性value:用于指定bean的 id。默认值是当前类名首字母改小写
//省略 getter/setter 和 tostring 方法
@Component(value="myperson") //value可省略:@Component("myperson")
public class person {
private String name;
private String age;
}
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
person hello01 = (person)applicationContext.getBean("myperson");
}
}
@Controller
:一般用在表现层(servlet包下的组件)@Service
:一般用在业务层(业务逻辑)@Repository
:一般用在持久层(数据库层)
@Component
:不属于以上几层
以上三个注解他们的作用和属性与Component是一模一样。
他们三个是spring框架为我们提供明确的三层使用的注解,使我们的三层对象更加清晰
在类上加入以上三个注解均可将类加入到IOC容器中,注解可以随便加,spring底层不会去验证注解是否是在那一层,是给程序员看的
创建的bean默认还是单实例的,若要改为多实例加入注解@scope(value="prototype")
二. 注入数据
作用与xml中bean标签中property标签的作用一致
@Autowired
:自动按照类型注入。
出现位置: 可以是变量上,也可以是方法上
在使用注解注入时,set方法就不是必须的了。
@Autowired 的原理:
先按照类型去容器中找到对应的组件
- 找到一个,就赋值。
- 没有找到,则报错。
- 找到了多个,按照成员变量的变量名作为id匹配(如一个类BookServiceExt继承了BookService,BookService组件就有多个,BookService的变量名是bookService,BookServiceExt的变量名是bookServiceExt,所以装配BookService)。
更改变量名后,此时没有id为bookServiceExt2,就没有匹配上,报错,解决方法如下:
@Qualifier
注解
作用:在自动按照类型注入的基础之上,再按照 Bean 的 id 注入。它在给字段注入时不能独立使用,必须和@Autowire 一起使用,但是给方法参数注入时,可以独立使用。
value:用于指定注入bean的id。
@Autowired 在方法上:
- 这个方法会在bean创建时自动运行
- 这个方法上的每一个参数都会自动注入值
@Autowired
@Resource
注解都是自动装配
@Autowired:spring自己的注解,只能在spring中庸
@Resource:java的标准,扩展性更强,若切换成其他框架也可以使用
集合类型的注入只能通过XML来实现。
三. spring新注解
@Configuration
:指定当前类是一个配置类
@ComponentScan
:通过注释指定当前spring在创建时扫描的包
@Configuration
@ComponentScan(basePackages="com.jess")
//相当于在xml中配置了<context:component-scan base-package="com.jess"></context:component-scan>
//有了注解后,xml中就不需要这句话了
public class configuration{
}
@Bean
:用于把当前方法的返回值作为bean对象存入IOC容器中
属性:name:指定bean的 id,不写默认方法名称
使用@Configuration 来注解类表示类可以被 Spring 的 IoC 容器所使用,作为 bean 定义的资源。
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
这和 Spring 的 XML 文件中的非常类似
<beans>
<bean id="myService" class="com.jess.services.MyServiceImpl"/>
</beans>
@Bean 注解扮演了和元素相同的角色。
如果 Bean 不是自己编写的类(如 JdbcTemplate、SessionFactoryBean 等),注释配置将无法实施,此时 XML 配置是唯一可用的方式。