Spring系列之新注解配置+Spring集成junit+注解注入
Spring系列之注解配置
Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率
你本来要写一段很长的代码来构造一个Beam对象,但是如果使用注解的话只要使用一个注解符号即可

下面我们来讲讲一些经常使用的注解符号
@Component 使用类上用于实例化Bean
@Controller 使用web层类上用于实例化Bean
@Service 使用在Service层类上用于实例化service
@Repository 使用在dao层类上用于实例化Bean
@Autorwired 使用在字段上用于根据类型依赖注入
@Qualifier 结合结合@Autowired一起使用根据名称进行依赖注入
@Resource 相当于@Autowired+@Qualifier一起使用
@Scope 标注bean的范围
@PostConstruct 使用该在方法上标注该方法是Bean的初始化方法
@PostDestory 使用在方法上标注该方法是Bean的销毁方法
这三个没有什么区别,不过是为了在后期读代码的时候更加利于我们区分,比如看到@Controller就知道这是web层,看到@Service就知道这是service层
@Component 使用类上用于实例化Bean
@Controller 使用web层类上用于实例化Bean
@Service 使用在Service层类上用于实例化service
@Repository 使用在dao层类上用于实例化Bean
所以我们只需讲一个即可,其他的使用方法均相同,这里我们使用@Component来讲解
定义一个userDaolmp类
package com.pjh.dao;
import com.pjh.dao.Imp.userdao;
import org.springframework.stereotype.Component;
@Component("userDaoImp")
public class userDaoImp implements userdao {
public void save() {
System.out.println("save");
}
}
定义一个测试类测试
@Test
public void test(){
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
userdao userDaoImp =(userdao) classPathXmlApplicationContext.getBean("userDaoImp");
userDaoImp.save();
}
然后我们就发现了如下报错
由蓝色的划线部分我们可以看出问题是:没有一个名叫userDaoImp的bean

这是为什呢?因为我们虽然写了注解,但是我们要让applicationContext.xml知道我们使用了注解,要告诉他哪里有个bean。所以我们在applicationContext中还需要加入以下配置
命名空间:xmlns:context="http://www.springframework.org/schema/context
约束路径:http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
注解的组件扫描:
<context:component-scan base-package="com.pjh"/>
成功运行

接下来我们再来讲讲如何使用注解进行注入
方式一:使用@Autowired+@Qualifier
目的:创建一个userdao类将其注入带service类中
userdao类代码
package com.pjh.dao;
import com.pjh.dao.Imp.userdao;
import org.springframework.stereotype.Component;
@Component("userDaoImp")
public class userDaoImp implements userdao {
public void save() {
System.out.println("save");
}
}
service类代码
package com.pjh.service.Imp;
import com.pjh.dao.Imp.userdao;
import com.pjh.dao.userDaoImp;
import com.pjh.service.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import javax.xml.bind.SchemaOutputResolver;
@Repository("serviceImp")
public class serviceImp implements service {
@Autowired
@Qualifier("userDaoImp")
private userDaoImp userDaoImp;
public void save() {
System.out.println("sssssss");
userDaoImp.save();
}
}
测试类
@Test
public void test(){
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
service service =(service) classPathXmlApplicationContext.getBean("serviceImp");
service.save();
}
结果

方式二:使用@Resource()
就是把 @Autowired + @Qualifier("userDaoImp")换成@Resource(name="userDaoImp"),其余代码均与方式一相同
package com.pjh.service.Imp;
import com.pjh.dao.Imp.userdao;
import com.pjh.dao.userDaoImp;
import com.pjh.service.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import javax.xml.bind.SchemaOutputResolver;
@Repository("serviceImp")
public class serviceImp implements service {
/* @Autowired
@Qualifier("userDaoImp")*/
@Resource(name="userDaoImp")
private userDaoImp userDaoImp;
public void save() {
System.out.println("sssssss");
userDaoImp.save();
}
}
结果

令人蛋碎的事情来了,报错了,意思是不支持版本5

解决方案:
File->setting下设置项目的jdk版本

File->ProjectStruct下设置版本

结果

又报错了,这。。。。。我tm心态崩了呀

根据报错的内容是报了个空指针异常,这是为什么呢?为什么使用@Autowired + @Qualifier("userDaoImp")不报错换成@Resource(name="userDaoImp")就报空指针呢
原因:jdk版本不支持
解决方案:
1.更换本地的jdk版本,最好jdk1.8以上,jdk9不支持有bug
2.再maven.pom文件中引入如下依赖
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-annotations-api</artifactId>
<version>7.0.47</version>
</dependency>
结果
成功运行
终于解决完了

使用@Scope注解标注Bean的范围
使用@Scope("prototype")
package com.pjh.dao;
import com.pjh.dao.Imp.userdao;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("userDaoImp")
@Scope("prototype")
public class userDaoImp implements userdao {
public void save() {
System.out.println("save");
}
}
测试代码
@Test
public void test(){
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
userdao userdao =(userdao) classPathXmlApplicationContext.getBean("userDaoImp");
userdao userdao1 =(userdao) classPathXmlApplicationContext.getBean("userDaoImp");
System.out.println(userdao);
System.out.println(userdao1);
}
结果

使用@Scope("singleton")
package com.pjh.dao;
import com.pjh.dao.Imp.userdao;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("userDaoImp")
@Scope("singleton")
public class userDaoImp implements userdao {
public void save() {
System.out.println("save");
}
}
结果

在这里顺便给大家复习复习singleton与prototype的区别吧
singleton
Bean的实例化个数:1个
Bean的实例化时机:当Spring核心配置文件被加载时
Bean的生命周期:
对象创建:当应用加载时对象创建
对象运行:只要容器在,对象就一直活着
对象销毁:当应用卸载,容器销毁时
prototype:在使用getBean方法的时候创建bean
prototype
Bean的实例化格式:多个
Bean的实例化时机:当调用getBean()方法时,实例化Bean
对象创建:当使用对象时,创建新的对象实例
对象运行:只要对象在使用中,对象就一直存在
对象销毁:对象长时间不使用,就会被java的垃圾回收机制回收
初始化方法和销毁方法
初始化方法
@PostConstract
@PostConstruct
public void constract(){
System.out.println("初始化方法");
}
销毁方法
@PostDestroy
@PreDestroy
public void destroy(){
System.out.println("在对象销毁前执行");
}
@Value()进行注入
package com.pjh.dao;
import com.pjh.dao.Imp.userdao;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Component("userDaoImp")
@Scope("singleton")
public class userDaoImp implements userdao {
public void save() {
System.out.println("save");
System.out.println("读取配置文件:"+one);
System.out.println("普通类型:"+a);
}
@Value("${one.one}")
private String one;
@Value("4")
private String a;
}
配置文件信息
one.one=1
applicationContext中添加的内容
<context:property-placeholder location="classpath*:one.properties"/>
测试代码
@Test
public void test(){
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
userdao userdao =(userdao) classPathXmlApplicationContext.getBean("userDaoImp");
userdao.save();
}
结果

使用上面的注解还不可以全部替代xml配置文件,还需要使用注解替代的配置如下
非自定义的Bean的配置:
加载properties文件的配置:<context:property-placeholder>
注解扫描的配置:context:component-scan
引入其他文件:
Spring新注解
@Configuration 用于指定当前类是一个Spring配置类,创建容器时会从该类上加载注解,该类不能是匿名类与final类
@ComponentScan 用于指定Spring在初始化容器的时候要扫描包
作用与Spring中的此代码相同:context:component-scan
@Bean 使用方法,将该方法的返回值返回到容器中
@Import 用于导入其他配置类
@PropertySource 用于加载properties文件中的配置
案例
核心配置类
package com.pjh.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
/*标志该类是核心配置类*/
@Configuration
/*扫描包*/
@Component("com/pjh")
/*导入其他配置类*/
@Import(DataSourceConfig.class)
public class Springconfig {
}
副配置类
package com.pjh.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;
/*加载配置文件*/
@PropertySource("classpath:jdbc.properties")
public class DataSourceConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
/*Spring会将当前方法的返回值以指定名称存储到Spring容器中*/
@Bean("dataSource")
public DataSource getDataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
return dataSource;
}
}
测试函数
@Test
public void test() throws SQLException {
ApplicationContext applicationContext = new
AnnotationConfigApplicationContext(Springconfig.class);
DataSource bean = (DataSource)applicationContext.getBean("dataSource");
Connection connection = bean.getConnection();
System.out.println(connection);
}
结果
成功创建connection连接

Spring集成junit
为什么使用Spring集成junit?
在测试类中每个类都要写下面的两行代码
ApplicationContext applicationContext = new
AnnotationConfigApplicationContext(Springconfig.class);
DataSource bean = (DataSource)applicationContext.getBean("dataSource");
这两行代码的作用是获取容器,不写的话报空指针异常
为了让我们测试的时候不用进行反复的写上述两行的操作,我们使用Spring来集成junit,用springjunit来创建spring容器,
我们只需将配置文件的名称告诉他们即可,将需要的bean直接在容器中进行注入
Spring集成junit的步骤
需要导入的jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
这是我spring系列的学习文章,我也会不断的学习,文章如有错误还请批评指正,如有帮助大家可以点点关注,我们一同学习

Spring系列之新注解配置+Spring集成junit+注解注入的更多相关文章
- JAVAEE——spring02:使用注解配置spring、sts插件、junit整合测试和aop演示
一.使用注解配置spring 1.步骤 1.1 导包4+2+spring-aop 1.2 为主配置文件引入新的命名空间(约束) 1.3 开启使用注解代替配置文件 1.4 在类中使用注解完成配置 2.将 ...
- 使用注解配置Spring
使用注解配置Spring 1.为主配置文件引入新的命名空间(约束) 2.开启使用注解代理配置文件 3.在类中使用注解完成配置 将对象注册到容器 修改对象的作用范围 值类型注入 引用类型注入 注意: 初 ...
- 注解配置spring
1.为什么使用注解配置Spring基于注解配置的方式也已经逐渐代替xml.这个是不可逆的潮流,所以我们必须要掌握使用注解的方式配置Spring 总结:(1)使用注解配置Spring,注解的作用就是用于 ...
- Spring中添加新的配置表,并对新的配置表进行处理
实习过程中boss交代的任务(以下出现的代码以及数据只给出小部分,提供一个思路) 目的:Spring中添加新的配置表,并对新的配置表进行处理:替换的新的配置表要友好,同时保证替换前后功能不能发生变化. ...
- Spring系列 之数据源的配置 数据库 数据源 连接池的区别
Spring系列之数据源的配置 数据源,连接池,数据库三者的区别 连接池:这个应该都学习过,比如c3p0,druid等等,连接池的作用是为了提高程序的效率,因为频繁的去创建,关闭数据库连接,会对性能有 ...
- Spring系列(六):Spring事务源码解析
一.事务概述 1.1 什么是事务 事务是一组原子性的SQL查询,或者说是一个独立的工作单元.要么全部执行,要么全部不执行. 1.2 事务的特性(ACID) ①原子性(atomicity) 一个事务必须 ...
- Spring系列(二):Spring IoC应用
一.Spring IoC的核心概念 IoC(Inversion of Control 控制反转),详细的概念见Spring系列(一):Spring核心概念 二.Spring IoC的应用 1.定义B ...
- Spring系列(三):Spring IoC源码解析
一.Spring容器类继承图 二.容器前期准备 IoC源码解析入口: /** * @desc: ioc原理解析 启动 * @author: toby * @date: 2019/7/22 22:20 ...
- 朱晔和你聊Spring系列S1E11:小测Spring Cloud Kubernetes @ 阿里云K8S
有关Spring Cloud Kubernates(以下简称SCK)详见https://github.com/spring-cloud/spring-cloud-kubernetes,在本文中我们主要 ...
随机推荐
- 原生js实现 vue的数据双向绑定
原生js实现一个简单的vue的数据双向绑定 vue是采用数据劫持结合发布者-订阅者模式的方式,通过Object.defineProperty()来劫持各个属性的setter,getter,在数据变动时 ...
- git 生成并添加 SSH key
git config --global user.name "wangjunqiang" git config --global user.email "wangjunq ...
- muduo源码解析4-exception类
exception class exception:public std::exception { }; 作用: 实现了一个异常类,继承于std::exception,主要用于实现打印线程调用栈信息. ...
- asp.net Core3.1自定义权限体系-菜单和操作按钮权限
我们在做项目项目,经常会碰到权限体系,权限体系属于系统架构的一个最底层的功能,也是非常重要的功能,几乎在每个项目都会用到.那么我们该如何设计一个比较合理的且扩展性较强的权限体系呢? 经过多天的摸索,参 ...
- 完美解决方案-雪花算法ID到前端之后精度丢失问题
最近公司的一个项目组要把以前的单体应用进行为服务拆分,表的ID主键使用Mybatis plus默认 的雪花算法来生成. 快下班的时候,小伙伴跑过来找我,:"快给我看看这问题,卡这卡了小半天了 ...
- java23种设计模式——五、建造者模式
源码在我的github和gitee中获取 目录 java23种设计模式-- 一.设计模式介绍 java23种设计模式-- 二.单例模式 java23种设计模式--三.工厂模式 java23种设计模式- ...
- lynx浏览器使用教程
http://www.wocaoseo.com/thread-216-1-1.html LYNX浏览器是谷歌官方推荐的一款文本浏览器,主要用来模拟蜘蛛看到您页面时候的样子,谷歌在网站站长指南中提到: ...
- Android Studio出现:Your project path contains non-ASCII 错误代码解决方法
导入Project的出现: Error:(1, 0) Your project path contains non-ASCII characters. This will most likely ca ...
- 还在写if/else if ... ?
在日常开发中,我们经常会写出很多 if else if ... 很多看起来又长又糟糕的代码, 那么策略模式你该去get 了. 点我查看哦!
- Fitness - 05.04
倒计时241天 运动38分钟,共计9组.拉伸10分钟. 每组跑步2分钟(6.3KM/h),走路2分钟(6KM/h). 上午下了课,直奔健身房. 手机坏了,没有听音乐. 没有吃午饭,但是上午喝的咖啡还是 ...