文章目录
Spring使用注解开发
使用注解开发的步骤:
-
导包
注意!在spring4之后要使用注解,必要要保证aop的包导入进项目了!
-
导入context约束(增加spring注解的支持)
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 注意要使用这个标签进行注解支持;以及上面的url导入约束 --> <context:annotation-config/> </beans>
@Autowired
自动装配!自动通过名字、类型进行容器中组件的装配
public class Person {
@Autowired
private Dog dog;
@Autowired
private Cat cat;
private String name;
}
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注意要使用这个标签进行注解支持;以及上面的url导入约束 -->
<context:annotation-config/>
<bean id="dog1" class="Dog"/>
<bean id="cat1" class="Cat"/>
<bean id="person" class="Person"/>
</beans>
@Qualifier(value=“xxx”)
名称匹配装配!与Autowired配合使用! 如果配置环境比较复杂,可以使用@Qualifier(value = “xxx”)注解来配合使用,value指定的是bean的id名!
public class Person {
@Autowired
@Qualifier(value="dog123")
private Dog dog;
@Autowired
@Qualifier(value="dog234")
private Dog dog;
@Autowired
private Cat cat;
private String name;
}
@Nullable
参数值可以为空
public class Person {
private String name;
public String getName() {
return name;
}
// 这样这个参数值就可以传进来空值而不报错了
public void setName(@Nullable String name) {
this.name = name;
}
}
@Component(重!基础!)
使类作为spring容器的组件!
指定要扫描的包(这些包中的类可以作为spring容器中的组件)
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:componment-scan base-pachage="com.qiu.pojo"/>
<context:annotation-config/>
</beans>
什么叫做容器中的组件?bean对象就是容器中的组件。也就是扫描了包后,确定为组件的包就可以直接作为bean对象,而不用在xml中显示声明一个bean对象了。对象名为类名小写。
上面提到了要将包中的类作为spring容器的组件bean对象,那么对要作为组件的类声明component注解就会在容器中生成对应的bean对象。
@Component
public class User{
public void said(){
System.out.println("123")
}
}
这样使用在xml中不需要显示声明bean对象就可以直接使用了!
public class Test{
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("beans");
User user = context.getBean("user", User.class)
}
}
@Value(“xxx”)
属性注入的注解!
@Component
public class User{
// 这个注解实现了xml中使用<property>标签进行注入的需求
@Value("hahaha")
public String name;
}
衍生注解
Component有几个衍生注解,与Component的功能是一样的,但是为了区分不同的业务类分了几个衍生注解
- Dao层:@Repository
- Service层:
- Controller层:
- 普通类:@Component
@Repository
用于Dao层。
@Repository
public class Student{
}
@Service
用于Service层。
@Service
public class StudentServiceImpl{
}
@Controller
用于Controller层。
@Controller
public class StudentServlet{
}
@Scope(“xxx”)
作用域注解! 与xml中配置的使用方法相同。
@Component
@Scope("singleton")
public class User{
}
@Component
@Scope("prototype")
public class Student{
}
使用JavaConfig脱离xml配置
使用java类的配置完全替代掉xml的配置!
JavaConfig是一个sprint的子项目,在spring4后成为了核心功能!
<beans>
<bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>
将xml文件替换为了Java类: xml能做的事情,Java配置类都可以做!
注意!@configuration实际上也是一个@Component,所以它也在spring容器中!
@Configuration
public class AppConfig {
// 例如创建一个bean
@Bean
public MyService myService1() {
return new MyServiceImpl();
}
}
使用:
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
MyService myService = ctx.getBean("myService1", MyService.class);
myService.doStuff();
}
@Bean
创建一个bean对象
@Configuration
@ComponentScan("com.qiu.pojo")
public class AppConfig {
// 例如创建一个bean
@Bean
// MyService:bean类型;myService:bean的id;MyServiceImpl:bean的实例类型(就是class)
public MyService myService() {
return new MyServiceImpl();
}
// 可以创建多个bean
@Bean
public MyService myService2() {
return new MyServiceImpl();
}
}
像之前在其他类中写一个@Component也可以作为一个bean对象,但是如果你需要多个这种类型的bean对象@Component就不可以实现了,所以可以使用@Bean实现!
@ComponentScan(“xxx”)
扫描包中的容器! 与<context:componment-scan base-pachage=“com.qiu.pojo”/>功能相同
@Configuration
@ComponentScan("com.qiu.pojo")
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
@Configuration
@ComponentScan("com.qiu.pojo")
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}