Spring常用注解
在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配置方法
<context:component-scan base-package="com.mashang"></context:component-scan>
@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销毁之前调用此方法
@Componentpublic class UserService { @PostConstruct public void init() {
<context:component-scan base-package="com.mashang"></context:component-scan>System.out.println("UserService is initialized"); } @PreDestroy public void destroy() {
<context:component-scan base-package="com.mashang"></context:component-scan>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 {
<context:component-scan base-package="com.mashang"></context:component-scan>return DriverManager.getConnection(url, accountName, password); }@Bean 注解的方法 getConnection() 返回的 Connection 对象会被注册为 Spring 容器中的 Bean,你可以通过 ctx.getBean() 来获取它
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]