02. 注解
1读虏、注解方式的實現(xiàn):
Java注解:使用事先定義好的注解標(biāo)簽责静,對類、方法盖桥、屬性的特征進行標(biāo)記灾螃,在編譯、運行時會找到對應(yīng)的類揩徊、方法腰鬼、屬性嵌赠,進行標(biāo)簽的執(zhí)行功能。
注解功能封裝在jar包中熄赡,導(dǎo)入Spring aop jar包即可
<!-- spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
添加xml文件約束,源碼包中找到對應(yīng)的 xsd文件姜挺。
修改配置文件,application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 開啟自動掃描注解本谜,base-package是需要掃描的包名 -->
<context:component-scan base-package="com.company.springPro"></context:component-scan>
2初家、標(biāo)簽的使用
2.1 注解創(chuàng)建對象
(1)@Component
對bean層對象的創(chuàng)建
例如:等于 <bean id="user" class="com.company.springPro.bean.UserPo"></bean>
@Component
public class UserPo {}
(2)@Repository
對數(shù)據(jù)庫操作層對象的創(chuàng)建
例如:等于 <bean id="userDao" class="com.company.springPro.dao.UserDao"></bean>
@Repository(value = "userDao")
public class UserDao {}
(3)@Service
對業(yè)務(wù)處理層對象的創(chuàng)建
例如:等于 <bean id="userService" class="com.company.springPro.service.UserService"></bean>
@Service(value="userService")
public class UserService {}
(4)@Controller
一般用在表現(xiàn)層
@Controller(value="userCtl")
public class UserController {}
2.2 對象創(chuàng)建的功能的擴展
(1)@Scope(value=“prototype”) 多例
(2)@Scope(value=“ singleton ”) 單例
@Component(value="user")
@Scope(value="prototype")
public class UserPo {}
2.3 注解注入屬性
(1)@Autowired
作用:自動按照類型注入,只要容器中有唯一的一個bean對象類型和要注入的變量類型匹配乌助,就可以注入成功溜在。
出現(xiàn)位置:可以是變量上,也可以是方法上他托。
細節(jié):在使用注解注入時掖肋,set方法就不是必須的了。
@Autowired
private UserDao userDao;
(2)@Qualifier:
作用:在按照類中注入的基礎(chǔ)之上再按照名稱注入赏参。它再給類成員注入時不能單獨使用志笼,但是給方法參數(shù)注入時可以。
屬性:value:用于注入bean的id
細節(jié):一般和@Autowired配合使用把篓。
@Autowired
@Qualifier("demoDaoImpl1")
private DemoDao demoDao;
(3)@Resource
作用:直接按照bean的id注入纫溃,它可以獨立使用。
屬性:name:用于指定bean的Id
@Resource(name="demoDaoImpl1")
private DemoDao demoDao;
以上三個注解都只能注入其他bean類型的數(shù)據(jù)韧掩,而基本類型和String類型無法使用上述注解實現(xiàn)紊浩。另外,集合類型的注入只能通過XML來實現(xiàn)疗锐。
(4)@Value
作用:用于注入基本類型和String類型的數(shù)據(jù)
屬性:value:用于指定數(shù)據(jù)的值坊谁,它可以使用spring中的SpEL(也就是spring的el表達式),SpEL的寫法${表達式}
@Value("ssss")
private String str;
@Value("${url}")
private String url;
2.4 用于改變作用范圍的
作用:他們的作用就和在bean標(biāo)簽中使用scope屬性實現(xiàn)的功能一樣的
@Scope
作用:用于指定bean的作用范圍
屬性:
value:指定范圍的取值滑臊。
常用取值:singleton(單例口芍,默認)、prototype(多例)
@Service
@Scope("prototype")
public class DemoServiceImpl implements DemoService {}
2.5 和生命周期相關(guān)的(了解)
作用:他們的作用就和在bean標(biāo)簽使用init-method 和 destory-method 的作用一樣
(1)@PreDestory
作用:用于指定銷毀方法
(2)@PostConstruct
作用:由于指定初始化方法
@PostConstruct
public void init(){
System.out.println("初始化成功");
}
@PreDestroy
public void destroy(){
System.out.println("銷毀成功");
}
2.6 配置類中的一些新注解
(1)@Configuration
作用:指定當(dāng)前類是一個配置類
細節(jié):當(dāng)配置類作為AnnotationConfigApplicationContext對象創(chuàng)建時雇卷,該注解可以不寫鬓椭。
@Configuration
public class SpringConfig {}
(2)@ComponentScan
作用:用于通過注解指定spring在創(chuàng)建容器時要掃描的包
屬性:
value:它和basePackages的作用是一樣的,都是用于指定創(chuàng)建容器時要掃描的包关划。
@ComponentScan("com.company")
public class SpringConfig {}
我們使用此注解就等于在xml中配置了:
<context:component-scan base-package="com.company"></context:component-scan>
(3)@Bean
作用:用于把當(dāng)前方法的返回值作為bean對象存入spring的ioc容器中小染。
屬性:name:用于指定bean的id,當(dāng)不寫時祭玉,默認值是當(dāng)前方法的名稱氧映。
細節(jié):當(dāng)我們使用注解配置方法時,如果方法有參數(shù)脱货,spring框架會去容器中查找有沒有可用的bean對象岛都。查找的方式和Autowired一樣律姨。
@Bean
public JdbcTemplate createJdbcTemplate(DataSource dataSource){
return new JdbcTemplate(dataSource);
}
@Bean(name="dataSource")
public DataSource createDataSource(){
DruidDataSource druidData = new DruidDataSource();
druidData.setDriverClassName("com.mysql.cj.jdbc.Driver");
druidData.setUrl("jdbc:mysql://127.0.0.1:3306/shop?characterEncoding=utf-8&serverTimezone=GMT");
druidData.setUsername("root");
druidData.setPassword("123456");
return druidData;
}
(4)@PropertySource
作用:用于指定 properties文件的位置
屬性:value:指定文件的路徑和名稱
關(guān)鍵字:classpath,表示在類路徑下
@PropertySource("classpath:jdbc.properties")
2.7 spring整合Junit
為什么要整合Junit臼疫?
(1)應(yīng)用程序的入口
main方法
(2)junit單元測試中择份,沒有main方法也能執(zhí)行
junit集成了一個main方法
該方法就會判斷當(dāng)前測試類中那些方法有 @Test 注解
junit就讓有Test注解的方法執(zhí)行
(3)junit不會管我們是否采用spring容器
在執(zhí)行測試方法時,junit根本不知道我們是不是使用了spring框架
所以也就不會為我們讀取配置文件/配置類創(chuàng)建spring核心容器
(4)由上面三點可知
當(dāng)測試方法執(zhí)行時烫堤,沒有IOC容器荣赶,就算寫了Autowired注解,也無法注入鸽斟。
如何整合拔创?
1、首先富蓄,需要導(dǎo)入jar包剩燥,必須保證junit的版本是4.12及以上
<!-- junit的jar包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- 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 Client {}
3立倍、告知spring的運行器灭红,spring和ioc創(chuàng)建是基于xml還是注解的,并且說明位置
@ContextConfiguration
locations:指定xml文件的位置口注,加上classpath關(guān)鍵字变擒,表示在類路徑下
classes:指定注解類所在位置
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class Client {}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = “classpath:bean.xml”)
public class Client {}