Spring注解开发
为什么使用注解开发Spring中的常用注解使用注解代替XML文件配置代替 Bean 标签的注解@Component代替id和class属性@Autowired、@Qualifier、@Resource代替字标签 property的依赖注入@Value代替property普通属性注入@Scope注解代替scope属性标注Bean的范围@PostConstruct、@PreDestroy注解代替init-method、destroy-method属性配置 代替其他标签的注解@Configuration@ComponentScan代替context:component-scan配置包扫描@Bean代替非自定义的Bean的配置@PropertySource代替context:property-placeholder加载外部properties配置文件大家好,我是【1+1=王】, 热爱java的计算机(人工智能)渣硕研究生在读。
如果你也对java、人工智能等技术感兴趣,欢迎关注,抱团交流进大厂!!!
Good better best, never let it rest, until good is better, and better best.
近期会重新温习一下SSM的相关知识,相应的博客会更新至专栏【SSM框架】中,欢迎大家关注!
SSM专栏:/weixin_43598687/category_11652306.html
为什么使用注解开发
首先你需要对注解有一定的了解才能进行下面的内容,对JAVA中的注解还比较陌生的小伙伴可以参考一下下面这篇文章,对JAVA注解有一个初步的了解。
【java中的注解及使用示例】—— Annotation原理知多少?:/weixin_43598687/article/details/122913951
从前面的内容我们可以发现,Spring开发中有许多的配置,非常的费时费力,影响开发效率。
Spring本身就是一个轻代码而重配置的框架,配置比较繁重,影响开发效率,所以就在Spring中加入了注解,使用注解代替原来的xml配置文件,这样就可以简化配置,提升开发效率。
Spring中的常用注解
@Component:使用在类上用于实例化Bean@Controller:使用在web层类上用于实例化Bean(作用于@Component一致)@Service:使用在service层类上用于实例化Bean(作用于@Component一致)@Repository:使用在dao层类上用于实例化Bean(作用于@Component一致)@Autowired:使用在字段上用于根据类型依赖注入@Qualifier:结合@Autowired一起使用用于根据名称进行依赖注入@Resource:相当于@Autowired+@Qualifier,按照名称进行注入@Value:注入普通属性@Scope:标注Bean的作用范围@PostConstruct:使用在方法上标注该方法是Bean的初始化方法@PreDestroy:使用在方法上标注该方法是Bean的销毁方法以上的这些注解主要是替代 Bean 标签的配置。但是这些注解还不能全部替代xml配置文件,包括以下几个新注解:
@Configuration:用于指定当前类是一个Spring 配置类,当创建容器时会从该类上加载注解@ComponentScan:用于指定Spring 在初始化容器时要扫描的包@Bean:使用在方法上,标注将该方法的返回值存储到Spring 容器中@PropertySource:用于加载.properties 文件中的配置@Import:用于导入其他配置类
使用注解代替XML文件配置
使用注解进行开发时,需要在核心配置文件中配置组件扫描,作用是指定哪个包及其子包下的Bean需要进行扫描以便识别使用注解配置的类、字段和方法。
也就是告诉Spring在这些包下面使用了注解,运行的时候需要扫描。
<context:component-scan base-package="com.wang"></context:component-scan>
base-package就是指向使用注解的包。
代替 Bean 标签的注解
前面我们讲过 Bean 标签的一些基本属性,如下:
1 - Spring快速入门(配置文件及API详解):/weixin_43598687/article/details/123128711
id:Bean实例在Spring容器中的唯一标识;class:配置Bean的全包名(全限定名);scope:配置对象的作用范围;init-method:指定类中的初始化方法名称;destroy-method:指定类中销毁方法名称。
现在我们就使用注解的方式来代替xml文件中 Bean 标签的配置。
@Component代替id和class属性
在xml中将一个类交给Spring容器管理的配置是这样的:
<bean id="userDao" class="com.wang.dao.impl.UserDaoImpl"></bean>
使用@Component注解后是这样的(直接在对应类上面加上@Component即可):
//<bean id="userDao" class="com.wang.dao.impl.UserDaoImpl"></bean>@Component("userDao")public class UserDaoImpl implements UserDao {
【注】:对于@Controller、@Service、@Repository三个注解,他们的作用与@Component注解是一致的,只是一个语义化的表达,体现出我们的三层架构(即@Controller对应controller层的类、@Service对应service层的类、@Repository对应dao层的类)
@Autowired、@Qualifier、@Resource代替字标签 property的依赖注入
xml文件中的依赖注入如下:
<bean id="userService" class="com.wang.service.impl.UserServiceImpl"><property name="userDao" ref="userDao"></property></bean>
使用注解代替如下:
@Resource注解相当于于@Autowired+@Qualifier注解,按照名称进行注入。
@Value代替property普通属性注入
xml中的值注入:
<bean id="user" class="com.wang.pojo.User"><property name="username" value="test"/><property name="password" value="testPassword"/></bean>
使用注解后的值注入:
@Component("user")public class User {private Integer id;@Value("test")private String username;@Value("testPassword")private String password;}
@Scope注解代替scope属性标注Bean的范围
xml中scope属性标注Bean范围
<bean id="userDao" class="com.wang.dao.impl.UserDaoImpl" scope="prototype"></bean>
使用@Scope注解标注Bean范围
@PostConstruct、@PreDestroy注解代替init-method、destroy-method属性配置
xml中配置Bean的初始化方法和销毁方法。
<bean id="user" class="com.wang.pojo.User" init-method="start" destroy-method="end"></bean>
@PostConstruct、@PreDestroy注解配置Bean的初始化方法和销毁方法。
@PostConstructpublic void start(){System.out.println("初始化方法");}@PreDestroypublic void end(){System.out.println("销毁方法");}
代替其他标签的注解
@Configuration
学习下面几个注解需要先创建一个类,并在该类上加@Configuration注解,表明该类是一个Spring的核心配置文件,以代替applicationContext.xml。
@Configuration//标志该类为Spring的核心配置类public class SpringConfig {}
@ComponentScan代替context:component-scan配置包扫描
xml中配置包扫描:
<context:component-scan base-package="com.wang"></context:component-scan>
配置类中配置包扫描:
@Configuration//标志该类为Spring的核心配置类@ComponentScan("com.wang")public class SpringConfig {}
@Bean代替非自定义的Bean的配置
xml中配置数据源如下:
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai"/><property name="username" value="root"/><property name="password" value="123456"/></bean>
配置文件中配置数据源:
@Configuration//标志该类为Spring的核心配置类@ComponentScan("com.wang")public class SpringConfig {@Bean("dataSource")public DataSource getDataSource(){DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai");dataSource.setUsername("root");dataSource.setPassword("123456");return dataSource;}}
@PropertySource代替context:property-placeholder加载外部properties配置文件
xml中加载.properties 文件中的配置
<context:property-placeholder location="jdbc.properties"/>
配置类中加载.properties 文件中的配置
获取jdbc.properties中配置的内容
@Configuration//标志该类为Spring的核心配置类@ComponentScan("com.wang")@PropertySource("jdbc.properties")public class SpringConfig {@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 getDataSource(){DruidDataSource dataSource = new DruidDataSource();// dataSource.setDriverClassName("com.mysql.jdbc.Driver");// dataSource.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai");// dataSource.setUsername("root");// dataSource.setPassword("123456");dataSource.setDriverClassName(driver);dataSource.setUrl(url);dataSource.setUsername(username);dataSource.setPassword(password);return dataSource;}}