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的销毁方法(容器关闭时才能看到,ApplicationContext变成ClassPathXmlApplicationContext 然后app.close()) |
若使用注解开发,则原本xml配置时
set依赖注入
@Autowired
@Qualifier(“userDao”)
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
可以删掉set方法
@Autowired //按照数据类型从Spring容器中进行匹配的
所以因为在@Repository(“userDao”)已经实力化了Bean在容器中,所以
@Qualifier(“userDao”)
private UserDao userDao;
Spring新注解
上面注解还不能全部替代以下配置:
- 非自定义的Bean的配置(即第三方jar包的类如:DruidDataSource):
- 加载properties文件的配置:context:property-placeholder
- 组件扫描的配置:context:component-scan
- 引入其他文件:
注解 | 说明 |
---|---|
@Configuration | 用于指定当前类是一个Spring配置类,当创建容器时会从该类上加载注解 |
@ComponentScan | 用于指定Spring在初始化容器时要扫描的包。 |
@Bean | 使用在方法上,标注该方法的返回值储存到Spring容器中 |
@PropertySource | 用于加载.properties文件中的配置 |
@Import | 用于导入其他配置类 |
新建一个类中使用,相当于用一个Configuration类替代ApplicationContext.xml文件
并且加上@Configuration注解声明是配置类
加载代码:
//ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);
使用案例
package config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
@Configuration
/* <!--配置组件扫描-->
<context:component-scan base-package="dao"></context:component-scan>
<context:component-scan base-package="service"></context:component-scan>
*/
@ComponentScan({"dao","service"}) //组件扫描
@Import(jdbcConfig.class)
public class SpringConfiguration {
}
package config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
import java.beans.PropertyVetoException;
/*
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
*/
@PropertySource("classpath:jdbc.properties") //加载配置文件
public class jdbcConfig {
@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") //返回值以此指定id储存到spring容器
public DataSource getDataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
return dataSource;
}
}