Spring-IOC容器中的常用注解與使用方法
Spring是什麼?
體系結構
引入Jar包
導入限制
常見注解
用于建立對象
用于注入資料
用于改變作用範圍
和生命周期相關(了解)
Spring5
Spring整合Junit
從IOC容器中擷取對象
Spring是什麼?
Spring是一個輕量級Java開發架構,最早有Rod Johnson建立,目的是為了解決企業級應用開發的業務邏輯層和其他各層的耦合問題。它是一個分層的JavaSE/JavaEE full-stack(一站式)輕量級開源架構,為開發Java應用程式提供全面的基礎架構支援。Spring負責基礎架構,是以Java開發者可以專注于應用程式的開發。
體系結構
核心容器(Core Container):Spring的核心容器是其他子產品建立的基礎,有Spring-core、Spring-beans、Spring-context、Spring-context-support和Spring-expression(String表達式語言)等子產品組成
資料通路/內建(Data Access)層:資料通路/內建層由JDBC、ORM、OXM、JMS和事務子產品組成。
Web層:Web層由Spring-web、Spring-webmvc、Spring-websocket和Portlet子產品組成。
AOP(Aspect Oriented Programming)子產品:提供了一個符合AOP要求的面向切面的程式設計實作,允許定義方法攔截器和切入點,将代碼按照功能進行分離,以便幹淨地解耦。
植入(Instrumentation)子產品:提供了類植入(Instrumentation)支援和類加載器的實作,可以在特定的應用伺服器中使用。
消息傳輸(Messaging):Spring4.0以後新增了消息(Spring-messaging)子產品,該子產品提供了對消息傳遞體系結構和協定的支援。
測試(Test)子產品:Spring-test子產品支援使用JUnit或TestNG對Spring元件進行單元測試和內建測試。
引入Jar包
<dependencies>
<!--spring的jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.11.RELEASE</version>
</dependency>
</dependencies>
導入限制
<?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">
<!--spring的限制 -->
<!--把對象的建立交給Spring來管理 -->
<!--擷取容器中對象時使用id-->
<!-- <bean id="accountServiceImpl" class="com.dynamic2.service.Impl.AccountServiceImpl"></bean>
<bean id="accountDaoImpl" class="com.dynamic2.dao.Impl.AccountDaoImpl"></bean>-->
<context:component-scan base-package="com.dynamic2"></context:component-scan>
</beans>
常見注解
用于建立對象
@Component:把資源讓spring來管理。相當于xml中配置一個bean。value:指定bean的id,如果不指定value屬性,預設bean的id是目前類的類名。首字母小寫
@Controller:與@Component功能一樣,一般用在表現層,便于分層
@Service:與@Component功能一樣,一般用在業務層,便于分層
@Repository:與@Component功能一樣,一般用于持久層,便于分層
/**
* @Author: Promsing
* @Date: 2021/3/19 - 11:34
* @Description: 用于建立對象
* @version: 1.0
* XML配置 <bean id="accountServiceImpl" class="com.dynamic2.service.Impl.AccountServiceImpl"></bean>
*/
@Repository("accountDao ")
public class AccountDaoImpl implements IAccountDao {
......
}
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
......
}
@Component("accountServiceImpl")
@Scope("prototype")//多例
public class AccountServiceImpl2 implements IAccountService {
......
}
用于注入資料
@Autowired:自動按照類型注入。當使用注解注入屬性時,set方法可以省略。它隻能注入其他bean類型。當有多個類型比對時。使用要注入的對象變量名稱作為bean的id,在spring容器中查找,找到了注入成功,找不到就報錯。
@Qualifier:在自動按照類型注入的基礎上,再按照Bean的id注入。它在給字段注入時不能單獨使用,必須和@Autowire一起使用;但是給方法參數注入時,可以單獨使用。value屬性是指定Bean的id
@Resource:直接按照Bean的id注入。它也隻能注入其他Bean類型。name屬性是指定Bean的id
@Value:注入基本資料類型和String類型資料
/**
* @Author: Promsing
* @Date: 2021/3/19 - 11:34
* @Description: 用于建立對象
* @version: 1.0
* XML配置 <bean id="accountServiceImpl" class="com.dynamic2.service.Impl.AccountServiceImpl"></bean>
*/
@Component("accountServiceImpl")
@Scope("prototype")//多例
public class AccountServiceImpl implements IAccountService {
//注入成員變量
/* @Autowired 自動按照類型注入--尋找類型
@Qualifier("accountDao2")*/ //尋找id
//以上兩個注解相加的作用等于這個
@Resource(name = "accountDao2")
private IAccountDao accountDao2;
@Override
public void saveAccount() {
accountDao2.saveAccount();
//System.out.println("service中的saveAccount執行了~~");
}
}
用于改變作用範圍
@Scope:指定Bean的作用範圍。value屬性指定範圍的值--singleton單例,prototype多例,request作用與web應用的請求範圍,session作用與web應用的會話範圍,global-session作用與叢集環境中會話範圍
@Component("accountServiceImpl")
@Scope("prototype")//多例
public class AccountServiceImpl implements IAccountService {
......
}
和生命周期相關(了解)
@PostConstruct:用于指定初始化方法
@PreDestroy:用于指定銷毀方法
Spring5
@Configuration:用于指定目前類是一個spring配置類,當有容器時會從該類上加載注解。擷取容器是使用AnnotationApplicationContext(有@Configuration注解的類.class)
@ComponentScan:用于指定spring在初始化容器時要掃描的包。作用和在spring的xml配置檔案找那個的<context : component-sacn base-package="com.dynamic"/>
@Bean:該注解隻用用在方法上,表明使用此方法建立一個對象,并且放入spring容器中
@Import:用于導入其他配置類,解耦合
/**
* @Author: Promsing
* @Date: 2021/3/28 - 0:36
* @Description: Spring配置類
* @version: 1.0
*/
@Configuration//指定目前類是一個配置類
@ComponentScan("com.dynamic_transaction_anno")//用于指定spring在初始化容器時需要掃描的包
@Import({JdbcConfig.class,TransactionConfig.class})//導入其他配置類
@EnableTransactionManagement//開啟spring注解事務的支援
public class SpringConfig {
@Bean("jdbcTemplate")
public JdbcTemplate createJdbcTemplate(DataSource ds){
return new JdbcTemplate(ds);
}
@Bean("dataSource")
public DataSource createDataSource(){
DriverManagerDataSource dr=new DriverManagerDataSource();
dr.setDriverClassName("com.mysql.jdbc.Driver");//com.mysql.jdbc.Driver
dr.setUrl("jdbc:mysql//localhost:330b/eesy");
dr.setUsername("root");
dr.setPassword("root");
return dr;
}
}
Spring整合Junit
@RunWith:替代原有的運作器
@ContextConfiguration:指定配置檔案的位置
@RunWith(SpringJUnit4ClassRunner.class)//替代原有運作器
@ContextConfiguration(classes=SpringConfiguration.class)//指定配置類
public class AccountServiceTest {
@Test
public void testFindAll(){
//執行測試方法
}
}
從IOC容器中擷取對象
/**
* @Author: Promsing
* @Date: 2021/3/21 - 11:22
* @Description: 單元測試
* @version: 1.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=SpringConfiguration.class)
public class AccountServiceTest {
@Resource(name = "accountServiceImpl")
private IAccountService accountService;
@Test
//從容器中擷取對象
public void test(){
//一、擷取容器
//使用配置檔案加載
ApplicationContext ac=new ClassPathXmlApplicationContext("bean3_1.xml");
//使用配置類加載
/// ApplicationContext ac=new AnnotationConfigApplicationContext(SpringConfiguration.class);
//二、擷取對象
accountService=(IAccountService)ac.getBean("accountServiceImpl",IAccountService.class);
//三、執行方法
List<Account> allAccounts = accountService.findAllAccount();
for (Account allAccount : allAccounts) {
System.out.println(allAccount);
}
}
}