一、注解的概述spring
注解是为了便于程序的调试而用于代替配置文件的一种程序语法,与配置文件具备互换性。一般基于注解编程的程序更加简洁。编程
(注:使用Spring注解必须导入aop包)框架
二、Spring组件注解工具
Spring组件注解做用在类上,用于声明该类为组件类。在Spring 框架启动时,自动扫描组件类建立实例对象并存放在Spring容器中。Spring的组件注解有单元测试
①@Controller:声明表示层的组件类; ②@Service:声明服务层的组件类;测试
③@Repository:声明持久层的组件类; ④@Component:声明其它的组件类。ui
(注:①Spring的4个组件注解在功能上是没有差异的,交换使用结果相同;②组件类的对象名默认为类名的首字母小写形式,组件注解的value属性能够设置对象名)this
三、<context:component-scan>标签——扫描器的配置编码
——base-package属性:指定须要扫描的包。spa
<context:component-scan base-package="cn"/>
四、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("--对象销毁--"); }
}
六、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:[文件名]”)
七、获取Spring注解容器的对象
@Test public void springTest() { //一、建立spring容器 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Myconfiguration.class); //二、获取Customer实例对象 Customer customer= context.getBean("customer", Customer.class); customer.save(); //三、调用对象的方法 context.close(); //四、关闭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 @ 万有引力+
-
-
-
-
-