Spring是轻代码而重配置的框架,注解代替xml配置文件可以简化配置,提高开发效率。
一、Spring原始注解
| 注解 | 说明 | 
|---|---|
| @Component | 使用在类上用于实例化Bean | 
| @Controller | 使用在web层类上用于实例化Bean | 
| @Service | 使用在service层类上用于实例化Bean | 
| @Repository | 使用在dao层类上用于实例化Bean | 
| @Autowired | 使用在字段上用于根据类型依赖注入 | 
| @Qualifier | 结合@Autowired一起使用用于根据名称进行依赖注入 | 
| @Resource | 相当于@Autowired+@Qualifier按照名称进行注入 | 
| @Value | 注入普通属性 | 
| @Scope | 标注Bean的作用范围 | 
| @PostConstruct | 使用在方法上标注该方法是Bean的初始化方法 | 
| @PreDestroy | 使用在方法上标注该方法是Bean的销毁方法 | 
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--注解的组件扫描-->
    <context:component-scan base-package="com.demo"/>
</beans>
示例
@Repository("testDao")
public class TestDaoImpl implements TestDao {
    @Override
    public void test() {
        System.out.println("test 方法被调用了。。。。");
    }
}
@Service("testService")
public class TestServiceImpl implements TestService {
    @Autowired
    @Qualifier("testDao")
    private TestDao testDao;
    @Override
    public void test() {
        testDao.test();
    }
}
@Test
 public void test6() throws Exception {
      ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
      TestService testService = (TestService) context.getBean("testService");
      testService.test();
 }
二、Spring新注解
| 注解 | 说明 | 
|---|---|
| @Configuration | 用于指定当前类是一个 Spring 配置类,当创建容器时会从该类上加载注解 | 
| @ComponentScan | 用于指定 Spring 在初始化容器时要扫描的包。 作用和在 Spring 的 xml 配置文件中 的 <context:component-scan base-package="要扫描的包路径"/>一样 | 
| @Bean | 使用在方法上,标注将该方法的返回值存储到 Spring 容器中 | 
| @PropertySource | 用于加载.properties 文件中的配置 | 
| @Import | 用于导入其他配置类 | 
示例
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://172.16.90.174:3306/dbName
jdbc.username=root
jdbc.password=123456
@Configurable
@ComponentScan("com.demo")
@Import(DataSourceConfiguration.class)
public class Configuration {
}
@PropertySource("classpath:jdbc.properties")
public class DataSourceConfiguration {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Bean("dataSource")
    public DataSource dataSource() throws Exception {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}
    @Test
    public void test7() throws Exception {
        ApplicationContext context = new AnnotationConfigApplicationContext(Configuration.class);
        ComboPooledDataSource dataSource = (ComboPooledDataSource) context.getBean("dataSource");
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        TestService testService = (TestService) context.getBean("testService");
        testService.test();
    }