Spring框架学习day02 基于注解的IOC配置

spring中ioc的常用注解

曾经xml的配置:
<bean id="accountService" class="cn.itcast.service.impl.AccountServiceImpl" scope="" init-method=" " destroy-method=" ">
	<property name="" value="" href=""></property>
</bean>
用于创建对象的注解:
 他们的作用就和在xml配置文件中编写一个<bean>标签实现的功能是一样的
 	Component:
 		作用:用于把当前类对象存入spring容器中
 		属性:
 		Value:用于指定bean的id。当我们不写时,它的默认值是当前类名,且首字母改小写
 	Controller:一般用在表现层
 	Service:一般用在业务层
 	Repository:一般用在持久层

代码如下:

业务层

@Service("accountService")
public class AccountServiceImpl implements AccountService {
}

持久层

@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
}
用于注入数据的:
	他们的作用就和在xml配置文件中的<bean>标签中写一个<property>标签的作用是一样的
	Autowired:
		作用:自动按照类型注入,只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功
			 如果Ioc容器中没有任何bean的类型和要注入的变量类型匹配,则报错
			 如果Ioc容器中有多个类型匹配时:使用 Resouce
		出现位置:
			可以是变量上,也可是方法上
		 细节:
			在使用注解注入时,set方法就不是必须的了
	Qualifier:
		作用:在按照类中注入的基础之上再按照名称注入,它在给类成员注入时不能单独使用。但是在给方法参数注入时可以
		属性:
			value:用于指定注入bean的id
	Resource:
		作用:直接按照bean的id注入,它可以独立使用
		属性:
			name:用于指定bean的id
			
 以上三个注入都只能注入其他bean类型的数据,而基本类型和String类型无法使用上述注解实现另外,集合类型的注入只能通过xml来实现
 
	value:
		作用:用于注入基本类型和String类型的数据
		属性:
			value:用于指定数据的值。它可以使用spring中SpEL(也就是spring的el表达式)
					SpEL的写法:${表达式}
代码如下

@Autowired:

@Autowired
private AccountDao accountDao ;

Qualifier:

@Autowired
@Qualifier(value = "accountService1")
private AccountService accountService;

Resource:

@Resource(name = "accountService1")
private AccountService accountService;

Value:

@Value("${jdbc.driver}")
private String driver;
用于改变作用范围的:
	他们的作用就和在<bean>标签中使用scope属性实现的功能是一样的
	Scope:
		作用:用于指定bean的作用范围
		属性:
			value:指定范围的取值。常用取值:singleton prototype

Scope:

@Autowired
@Scoper(value = "prototype")
private AccountService accountService;

spring中的其他注解

 	Configuration:
 		作用:指定当前类是一个配置类
@Configuration
public class SpringConfiguration {
}
 	ComponentScan:
 		作用:用于通过注解指定spring在创建容器时要扫描的包
 	 	属性:
 			value:它和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包
@Configuration
@ComponentScan("cn.itcast")
public class SpringConfiguration {
}

//我们使用此注解就等同于在xml中配置了以下标签:
	<context:component-scan base-package="cn.itcast"></context:component-scan>
	Bean:
		作用:用于把当前方法的返回值作为bean对象存入spring的ioc容器中
		属性:
			name:用于指定bean的id。当不写时,默认值为方法的名称
		细节:
			当我们使用注解配置方法时,如果方法有参数,spring框架会去容器中查找有没有bean对象
			查找的方式和Autowired注解的作用是一样的
//配置QueryRunner
@Bean(name = "runner")
public QueryRunner getQueryRunner(DataSource dataSource){
	return new QueryRunner(dataSource);
}
	Import
		作用:用于导入其他的配置类
		属性:
			value:用于指定其他配置类的字节码
	当我们使用Import的注解之后,有Import注解的类就是父配置类,而导入的都是子配置类

 	PropertySource
		作用:用于指定properties文件的文职
		属性:
		value:指定文件的名称和路径
			关键字:classpath,表示类路径下
@Configuration
@ComponentScan("cn.itcast")
@Import(JdbcConfig.class)
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {
}

Spring 整合 Junit 的问题分析

    1.应用程序的入口
        main方法
    2.Junit单元测试中,没有main方法也能执行
        Junit集成了一个main方法
        该方法就会判断当前测试类中哪些方法有 @Test注解
        Junit就让有Test注解的方法执行
    3.Junit不会管我们是否采用Spring框架
        在执行测试方法时,Junit根本不指定我们是不是使用了Spring框架
        所有也就不会为我们读取配置文件/配置类创建Spring核心容器
    4.由以上三点可知
        当测试方法执行时,没有Ioc容器,就算写了Autowired注解,也无法实现注入

解决:
Spring整合junit的配置

1.导入Spring整合的junit的jar(坐标)
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
2.使用Junit提供的一个注解把原有的main方法替换了,替换成spring提供的 @Runwith
@RunWith(SpringJUnit4ClassRunner.class)
public class AccountTest {
告知spring的运行器,spring和ioc创建是基于xml还是注解的,并且说明位置
	@ContextConfiguration
		location:指定xml文件的位置,加上classpath关键字,表示在类路径下
		classes:指定注解类所在地位置

指定注解类所在地位置:

扫描二维码关注公众号,回复: 10642839 查看本文章
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountTest {
}

指定 XML 文件所在地位置

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(location = "classpath:bean.xml")
public class AccountTest {
}

注意:

当我们使用Spring 5.x版本的时候,要求Junit的jar必须是4.12及以上
发布了17 篇原创文章 · 获赞 0 · 访问量 150

猜你喜欢

转载自blog.csdn.net/weixin_46539792/article/details/105328686
今日推荐