Spring的基础注解
1、注解的概述
注解是为了便于程序的调试而用于代替配置文件的一种程序语法,与配置文件具有互换性。通常基于注解编程的程序更加简洁。
(注:使用Spring注解必须导入aop包)
2、Spring组件注解
Spring组件注解作用在类上,用于声明该类为组件类。在Spring 框架启动时,自动扫描组件类创建实例对象并存放在Spring容器中。Spring的组件注解有
①@Controller:声明表示层的组件类; ②@Service:声明服务层的组件类;
③@Repository:声明持久层的组件类; ④@Component:声明其它的组件类。
(注:①Spring的4个组件注解在功能上是没有差别的,交换使用结果相同;②组件类的对象名默认为类名的首字母小写形式,组件注解的value属性可以设置对象名)
3、<context:component-scan>标签——扫描器的配置
——base-package属性:指定需要扫描的包。
<context:component-scan base-package="cn"/>
4、Spring依赖注入的注解
(1)@Autowired注解——自动注入容器的对象
①在属性上注入:注入该属性; ②在方法上注入:注入该方法的参数; ③在构造方法上注入:注入该构造方法的参数;
(注:①@Autowired 注解默认允许注入的对象为空,可以通过equired属性设置;②添加@Autowired 注解的方法和构造方法在项目启动的时候将自动执行;③无参的方法和构造方法不支持使用@Autowired注解)
(2)@Qualifier注解——设置注入的对象名
——value属性:指定注入 Spring 容器中的对象名;
(注:@Autowired没有指定对象名的属性,只能通过@Qualifier注解指定容器中的对象名)
(3)@Resource注解——注入容器的对象
——name属性:指定注入 Spring 容器中的对象名;
(注:①@Resource注解在功能上是@Autowired和@Qualifier注解的合并注解;②@Resource不能在构造方法上注入)
(4)@Value注解——注入标量数据
——@Value注解用于注入标量数据,并支持注入Properties文件的键值对。
public class Manager { @Value("zhangsan")
private String name; @Value("${manager.age}")
private int age; @Autowired
@Qualifier(value="customer01")
private Customer customer = null; @Autowired(required=false)
public Manager(Customer customer) {
super();
this.customer = customer;
} @Resource(name="customer01")
public void saveCustomer(Customer customer) {
this.customer = customer;
} }
①@Scope注解——指定示例对象的生命周期
——value属性:设置创建的Spring对象的生命周期;
②@PostConstruct注解——指定Spring对象的初始化方法;
③@PreDestroy注解——指定Spring对象的销毁方法。
@Service
@Scope(value="singleton")
public class CustomerService { @PostConstruct
public void init(){
System.out.println("--对象初始化--");
} @PreDestroy
public void destroy(){
System.out.println("--对象销毁--");
}
}
6、Spring纯注解配置
①@Configuration注解
该注解标示当前类是Spring 的配置类,该类用于代替Spring的XML配置文件;
②@ComponentScan注解
该注解用于配置扫描Spring组件类的路径;
——value/basePackages属性:用于指定要扫描的包;
③@PropertySource注解
该注解用于配置需要加载的Properties文件;
——encoding属性:指定编码格式;
——value属性:指定 properties 文件位置;
④@Bean注解
该注解用于将通过方法创建的对象存放在Spring容器中;
——name属性:设置对象名;
⑤@Import注解
该注解用于导入其他配置类;
——value属性:指定配置类。
@Configuration
@ComponentScan(basePackages="cn")
@Import(value=Config.class)
@PropertySource(encoding="UTF-8",value="classpath:sys.properties")
public class Myconfiguration { @Bean
public Date getDate() {
return new Date();
} }
(注:①基于纯注解声明的对象将存放在AnnotationConfigApplicationContext容器中②在需要指定文件的路径时,当文件在类路径下时其路径为“classpath:[文件名]”)
7、获取Spring注解容器的对象
@Test
public void springTest() { //1、创建spring容器
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Myconfiguration.class); //2、获取Customer实例对象
Customer customer= context.getBean("customer", Customer.class); customer.save(); //3、调用对象的方法 context.close(); //4、关闭Spring容器,释放资源
}
Junit是单元测试工具,Spring 框架提供test包对 Junit 单元测试工具进行了整合。
①@RunWith注解
——value属性:指定使用的单元测试执行类;
②@ContextConfiguration注解
——classes属性:指定配置类。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=Myconfiguration.class)
public class SpringTest{ @Autowired
private Customer customer; @Text
public void springTest() {
customer.save();
} }
———————————————————————————————————————————————————————————————————
The end @ 万有引力+
-
-
-
-
-