使用注解开发
说明
在spring4之后,想要使用注解形式,必须得要引入aop的包

在配置文件当中,还得要引入一个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">
beans>
Bean的实现
我们之前都是使用 bean 的标签进行bean注入,但是实际开发中,我们一般都会使用注解!
1、配置扫描哪些包下的注解
<context:component-scan base-package="com.kuang.pojo"/>
2、在指定包下编写类,增加注解
@Component("user")
// 相当于配置文件中
public class User {
public String name = "张三";
}
3、测试
@Test
public void test(){
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("beans.xml");
User user = (User) applicationContext.getBean("user");
System.out.println(user.name);
}
属性注入
使用注解注入属性
1、可以不用提供set方法,直接在直接名上添加@value(“值”)
@Component("user")
// 相当于配置文件中
public class User {
@Value("张三")
// 相当于配置文件中
public String name;
}
2、如果提供了set方法,在set方法上添加@value(“值”);
@Component("user")
public class User {
public String name;
@Value("张三")
public void setName(String name) {
this.name = name;
}
}
衍生注解
我们这些注解,就是替代了在配置文件当中配置步骤而已!更加的方便快捷!
@Component三个衍生注解
为了更好的进行分层,Spring可以使用其它三个注解,功能一样,目前使用哪一个功能都一样。
@Controller:web层
@Service:service层
@Repository:dao层
写上这些注解,就相当于将这个类交给Spring管理装配了!
作用域
@scope
singleton:默认的,Spring会采用单例模式创建这个对象。关闭工厂 ,所有的对象都会销毁。
prototype:多例模式。关闭工厂 ,所有的对象不会销毁。内部的垃圾回收机制会回收
@Controller("user")
@Scope("prototype")
public class User {
@Value("张三")
public String name;
}
小结
XML与注解比较
xml与注解整合开发 :推荐最佳实践
xml管理Bean
注解完成属性注入
使用过程中, 可以不用扫描,扫描是为了类上的注解
<context:annotation-config/>
作用:
基于Java类进行配置
JavaConfig 原来是 Spring 的一个子项目,它通过 Java 类的方式提供 Bean 的定义信息,在 Spring4 的版本, JavaConfig 已正式成为 Spring4 的核心功能 。
测试:
1、编写一个实体类,Dog
@Component //将这个类标注为Spring的一个组件,放到容器中!
public class Dog {
public String name = "dog";
}
2、新建一个config配置包,编写一个MyConfig配置类
@Configuration //代表这是一个配置类
public class MyConfig {
@Bean //通过方法注册一个bean,这里的返回值就Bean的类型,方法名就是bean的id!
public Dog dog(){
return new Dog();
}
}
3、测试
@Test
public void test2(){
ApplicationContext applicationContext =
new AnnotationConfigApplicationContext(MyConfig.class);
Dog dog = (Dog) applicationContext.getBean("dog");
System.out.println(dog.name);
}
4、成功输出结果!
导入其他配置如何做呢?
1、我们再编写一个配置类!
@Configuration //代表这是一个配置类
public class MyConfig2 {
}
2、在之前的配置类中我们来选择导入这个配置类
@Configuration
@Import(MyConfig2.class) //导入合并其他配置类,类似于配置文件中的 inculde 标签
public class MyConfig {
@Bean
public Dog dog(){
return new Dog();
}
}
一.用于创建对象的注解
作用:和在xml配置文件中编写一个标签实现的功能一样。
1.@Component : 用于把当前类对象存入Spring容器中。
属性:value — 用于指定bean的id。如果不写该属性,id的默认值是当前类名,且首字母改为小写。
2.@Controller : 一般用在表现层。
3.@Service : 一般用在业务层。
4.@Repository : 一般用在持久层(dto层)。
备注:其中2,3,4注解的作用和属性与@Component 一模一样,他们三个是Spring框架为我们提供的三层架构使用的注解,使我们对三层对象更加清晰。
二.用于注入数据的注解
作用:和在xml配置文件中的标签中写一个标签的功能一样。
1.@Autowired : 自动按照类型注入。只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功。可以作用在变量或者方法上。
2.@Qualifier : 在按照类型注入的基础之上再按照名称注入,它在给类成员注入时要和@Autowired配合使用,但是在给方法参数注入是可以单独使用。
3.@Resource : 直接按照bean的id注入,可以独立使用。
属性:name — 用于指定bean的id。
备注:以上三个注入都只能注入其他bean类型的数据,而基本类型和String类型无法使用上述注解实现。另外,集合类型的注入只能通过xml来实现。
4.@Value : 用于注入基本类型和String类型的数据。
属性:value — 用于指定数据的值,它可以使用Spring中的SpEL(也就是Spring中的el表达式)。SpEL的写法:${表达式}
三.用于改变作用范围的注解
作用:和在xml配置文件中的标签中使用scope属性实现的功能一样。
1.@Scope : 用于指定bean的作用范围。
属性:value — 指定范围的取值。常用取值:singleton(单例)和prototype(多例)。
四.和生命周期相关的注解
作用:和在xml配置文件中的标签中使用init-method和destory-method属性实现的功能一样。
1.@PreDestory : 用于指定销毁方法。
2.@PostConstruct : 用于指定初始化方法。
五.Spring新注解
1.@Configuration : 用于指定当前类是一个配置类。
2.@ComponentScan : 用于通过注解指定Spring在创建容器时要扫描的包。
3.@Bean : 用于把当前方法的返回值作为bean对象存入Spring的IOC容器中。
属性:name — 用于指定bean的id。当不写时,默认值为当前方法的名称。
4.@Import : 用于导入其他的配置类。
属性:value — 用于指定其他配置类的字节码。当我们使用@Import注解时,有@Import注解的类就是父配置类。
5.@PropertySource : 用于指定properties文件的位置。
属性:value — 指定文件的名称和路径。关键字classpath表示类路径下。