在Spring中,有许多高效的注解,其简化了开发并提高代码可读性,这样我们就不用再去
spring.xml
文件中写标签了非常方便
在Spring,有用于识别不同类型的Bean,使得Spring容器可以自动管理这些Bean的创建和生命周期
@Component
作用:将一个类标记为Spring容器管理的Bean,Spring会自动扫描并创建该对象
用法:在类上使用@Component
注解,Spring会自动注册为一个Bean
//默认将id名改为小写
//可使用value属性修改
//@Component(value = "a")
//@Component("s")
@Component
public class Admin implements Serializable {
...
}
配置方法
在配置类或XML配置文件中启用组件扫描(
@ComponentScan
或标签
Java配置方法
@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
}
XML配置方法
@Service
作用:@Service
是@Component
的派生注解,主要用于业务层的类(ServiceImp),表示该类是一个服务组件,其更加见名知意,表明是服务层的Bean
用法:与@Component
用法相同
配置:与@Component
相同使用配置类或XML文件
@Service("adminService")
public class AdminServiceImpl implements AdminService {
...
}
@Repository
作用:@Repository
是@Component的另一个派生注解,用于标记数据访问层(DAO)
用法:与@Service
一致,特别的是@Repository
是针对持久化层的
@Repository
public class UserRepository {
...
}
配置方法:在配置类中启用组件扫描,或者使用 XML 配置
@Controller
作用:@Controlller是用于标记Spring MVC控制器类的注解.其表明该类是一个Web控制器.用于处理请求返回视图
用法:与Spring MVC一起使用,用于处理HTTP请求
@Controller
public class UserController {
....
}
@Autowired
@Autowired
是最常用的注解,用于将Spring容器中的Bean自动注入到当前类中
@Service("adminService")
public class AdminServiceImpl implements AdminService {
@Autowired
private AdminDao adminDao;
}
在需要注入的字段上添加@Autowired
即可
@Qualifier
@Qualifier
用于与@Autowired
一起使用,在存在多个类型相同的Bean时,即可提供@Qualifier
来指定具体注入的Bean@Repository
public class ClassDao1 extends ClassDao{}
@Repository
public class ClassDao2 extends ClassDao{}
@Autowired
@Qualifier("classDao1")
private ClassDao classDao;
public void queryAll() {
dao.queryAll();
System.out.println(classDao);
}
@Resource
@Resource
可以注入Bean,但通常是按照名称注入,其与@Autowired
有相似之处,但在注入方式上有所不同
这是@Resource
的默认注入方式,其会查找容器中释放有名称与目标属性名相同的Bean,并将其注入
public class AdminServiceImpl implements AdminService {
@Resource(name = "adminDao")
private AdminDao adminDao;
}
在这个例子中,@Resource(name = "adminDao")
会根据 adminDao
的名字注入对应的 AdminDao
实现类
若没有指定name属性,@Resource
会根据属性的类型自动注入,也就是说,如果@Resource
,注解的目标与容器中某个类型匹配,其会自动注入该类型的Bean
public class AdminServiceImpl implements AdminService {
@Resource
private AdminDao adminDao;
}
这个例子中,若容器中存在AdminDao
类型的Bean,Spring会自动注入到adminDao
属性中
@Resource
和@Autowired
的差异@Autowired
默认按类型注入,可使用@Qualifier
来指定特定Bean名称,解决滴哦共和Bean的问题@Resource
默认按名称注入,如果为指定name,则按类型进行注入.@PostConstruct
和@PreDestroy
两个注解分别为再Bean初始之后和销毁之前执行的方法
用法:
@PostConstruct
:当Bean再创建并且所有依赖注入完成之后,调用该方法@PreDestroy
:在Bean销毁之前调用此方法@Component
public class UserService {
@PostConstruct
public void init() {
System.out.println("UserService is initialized");
}
@PreDestroy
public void destroy() {
System.out.println("UserService is destroyed");
}
}
@Configuration
与@ComponentScan
@Configuration
的作用:使用这个注解标识的类表明,这个类是一个Spring
的配置类,这种配置类相比于传统的使用XML文件配置文件来说,Spring
配置类通常包括Bean的定义,和配置Bean的方法并将其注册到Spring容器中@ComponentScan
的作用:用于指定在Spring初始化容器时应该扫描的包,并将包中的类注册为Bean.其作用类似于XML文件中的
标签@Configuration
@ComponentScan("com.mashang")
//@ComponentScan({"com.mashang"})配置多个路径的方法
public class SpringConfig {
}
@Import
@Configuration
@ComponentScan("com.mashang")
@Import({DruidConfig.class, Test01.class})
public class SpringConfig {
}
@PropertySource
与@Value
@PropertySource
@PropertySource
用于配置Spring要加载的.properties
文件@Component
@PropertySource("classpath:druid.properties")
public class JDBCUtil {
...
}
classpath
:用于接收改文件位于项目内的类路径@Value
作用:用于将配置文件中的值注入到Spring Bean字段,方法参数或构造函数中.通常与@PropertySource
一起使用,通过${}
语法引用.properties
中的键值对
Component
@PropertySource("classpath:druid.properties")
public class JDBCUtil {
@Value("${driverClassName}")
private String driver;
@Value("${url}")
private String url;
@Value("${accountName}")
private String accountName;
@Value("${password}")
private String password;
}
@Bean
@Bean
注解是作用是一个方法上的,被标识的方法的返回值会被注册为Spring Bean,并放入到容器中.name
属性用于指定改Bean的id,若不指定默认为方法名@Bean
public Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, accountName, password);
}
@Bean
注解的方法getConnection()
返回的Connection
对象会被注册为 Spring 容器中的 Bean,你可以通过ctx.getBean()
来获取它
参与评论
手机查看
返回顶部