Spring 注解开发笔记
常用依赖
<!--注入这一个依赖即可同时注入AOP,Context等-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
常用注解
@Value
注入值,类型为基本类型或String才可以使用此注解。
@AutoWired
- 实现bean的自动装配,Spring根据bean的class类型进行搜索。
- 若class不唯一,则需要配合@Qualifier,其值为bean的ID值。
- 若以上均不满足,则会报错。
@Resource
- 首先自动根据类型查找,若找不到再根据name字段搜索bean,都找不到再报错。
- 类似@AutoWired与@Qualifier的组合,javax.annotation下的注解,非Spring Annotation下的。
@Component
组件,放在类上注解,表示该类被Spring管理了。其有如下三个分支,配合web mvc开发分类:
@Service
表明该类为MVC中Service层的类
@Repository
表明该类为MVC中的Dao层的类
@Controller
表明该类为MVC中的Web(Servlet)层的类
例子
@Component(value = "consumer")
public class Consumer {
@Value(value = "userno001")
private String consumerid;
@Value(value = "admin@gmail.com")
private String consumermail;
@Value(value = "root7747")
private String consumerpassword;
private int consumerlevel;
private double money;
private double payoutsum;
private double discountrate;
private double integral;
private int root;
@Autowired(required = "false") //一般不这么用
@Qualifier(name = "cat")
private Cat cat;
@Resource(name = "the_dog")
private Dog dog;
……
}
PS : 要让注解生效,必须在对于xml或java对象中开启注解配置。以xml为例:
<context:annotation-config/>
<context:component-scan base-package="com.spring_review.model,com.spring_review.service"/>
xml 方式以及注解方式的优缺点
- xml : 配置万能,便于维护,但配置麻烦
- annotation :配置简单,维护麻烦,不是自己的类无法使用。
- 一般使用xml来管理bean,而使用注解来注入属性。将核心类在xml里注册,其它可酌情使用注解。
Spring 5 纯注解开发笔记
@Configuration
在一个Java类上方标注,表明该类为Spring的配置类,作用等用于applicationContext.xml这种基于xml 的Spring配置文件中的’……’。其本质相当于一个组件Component
@Bean
相当于xml中的’<bean id="" …>…’ 。在方法前标注,方法名即对应xml中的id属性值;方法的返回值即xml中的class属性。
@ComponentScan
扫描器。
相当于xml中的:
<context:annotation-config/>
<context:component-scan base-package="对应全类名坐标"/>
在类上方添加此注解,value属性输入欲扫描的包坐标
@ImportResource
相当于xml中的’’,导入其他配置文件。
@Import
导入其他Java配置类,主动导入的为主配置类。
测试
Cat、SP 类略去
package com.config;
import com.spring_review.model.Cat;
import org.springframework.context.annotation.*;
import java.util.Date;
@Configuration
@ComponentScan(value = {"com.spring_review.model"})
@Import(value = {SpringDaoConfig.class})
public class SpringConfig {
@Bean
public Date GetDate(){
return new Date();
}
@Bean
public Cat GetMyCat(Date date){
Cat cat = new Cat();
cat.setBirthday(date);
return cat;
}
}
@Configuration
public class SpringDaoConfig {
@Bean
public SP CreateSP(){
return new SP();
}
}
@Test
public void mytest(){
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
Consumer consumer = context.getBean(Consumer.class);
if(consumer!=null){
System.out.println(consumer);
System.out.println(consumer.GetCat());
}
SP sp = context.getBean("CreateSP",SP.class);
if(sp!=null){
System.out.println(sp);
}
Cat cat = context.getBean("GetMyCat",Cat.class);
System.out.println(cat + " " + cat.getBirthday().toString());
}