轉(zhuǎn)載請(qǐng)注明來源 賴賴的博客
導(dǎo)語
簡(jiǎn)單的不一定是好的,復(fù)雜的也不一定是好的柒巫,合適的才是最好的励堡。
通過增加Spring注解從而簡(jiǎn)化Xml配置,并提供實(shí)例堡掏。
建議先了解Spring ApplicationContext的xml配置方式应结。可參考03 Spring IoC之對(duì)象間的相互關(guān)系和 Spring 應(yīng)用結(jié)構(gòu)
實(shí)例
如果看過 03 Spring IoC之對(duì)象間的相互關(guān)系和 Spring 應(yīng)用結(jié)構(gòu) 可以跳到項(xiàng)目詳解這一節(jié)泉唁,因?yàn)榻Y(jié)構(gòu)和輸出結(jié)構(gòu)與上一節(jié)一致摊趾。
項(xiàng)目工程目錄結(jié)構(gòu)和代碼獲取地址
獲取地址(版本Log將會(huì)注明每一個(gè)版本對(duì)應(yīng)的課程)
https://github.com/laiyijie/SpringLearning
目錄結(jié)構(gòu)
運(yùn)行工程
運(yùn)行具有Main函數(shù)的 App.java
得到如下輸出
false
工程簡(jiǎn)單分層(如果看過 03 Spring IoC之對(duì)象間的相互關(guān)系和 Spring 應(yīng)用結(jié)構(gòu) 可以略過到下一節(jié))
現(xiàn)在工程有三個(gè)包,包之間的關(guān)系如下:
- 應(yīng)用層:
me.laiyijie.demo
, 只調(diào)用服務(wù)層 - 服務(wù)層:
me.laiyijie.demo.service
游两,只調(diào)用數(shù)據(jù)層 - 數(shù)據(jù)層:
me.laiyijie.demo.dataaccess
,最底層漩绵,直接操作持久化數(shù)據(jù)(文件贱案、數(shù)據(jù)庫等)
項(xiàng)目詳解
從App.java入手
App.java(與上一節(jié)比無變化)
package me.laiyijie.demo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import me.laiyijie.demo.service.UserService;
public class App {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("root-context.xml");
UserService userService = context.getBean(UserService.class);
System.out.println(userService.login("lailai", "laiyijie","127.0.0.1"));
context.close();
}
}
Main函數(shù)中依舊只有四行代碼,這里引用到了UserService
止吐,并且調(diào)用了UserService
的login
方法宝踪。
我們來看下UserService
UserService.java(與上一節(jié)比無變化)
package me.laiyijie.demo.service;
public interface UserService{
boolean login(String username,String password,String ip);
}
OK,非常簡(jiǎn)單碍扔,只是一個(gè)接口瘩燥,定義了有這么一個(gè)方法,具體實(shí)現(xiàn)是在同一個(gè)包下面的另一個(gè)文件不同,UserServiceImpl.java中
UserServiceImpl.java(變化來了@靼颉)
package me.laiyijie.demo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import me.laiyijie.demo.dataaccess.AccountAccess;
import me.laiyijie.demo.dataaccess.LoginLogAccess;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private AccountAccess accountAccess;
@Autowired
private LoginLogAccess loginLogAccess;
public boolean login(String username, String password,String ip) {
if (!accountAccess.isAccountExist(username)) {
return false;
}
if (accountAccess.isPasswordRight(username, password)) {
accountAccess.updateLastLoginTime(username);
loginLogAccess.addLoginLog(username, ip);
return true;
}
return false;
}
}
在這里新增了兩個(gè)注解 @Service、@Autowired 二拐,減少了getter和setter
-
@Service 在類前注釋服鹅,將此類實(shí)例化,等同于在root-context中配置
<bean class="me.laiyijie.demo.service.UserServiceImpl"> </bean>
-
@Autowired 在屬性前配置百新,將屬性對(duì)應(yīng)的對(duì)象從工廠中取出并注入到該bean中企软,等同于
<property ref="accountAccessImpl" name="accountAccess"></property> <property ref="loginLogAccessImpl" name="loginLogAccess"></property>
兩個(gè)注解組合起來等同于:
<bean class="me.laiyijie.demo.service.UserServiceImpl">
<property ref="accountAccessImpl" name="accountAccess"></property>
<property ref="loginLogAccessImpl" name="loginLogAccess"></property>
</bean>
那么,讓我們看一下root-context.xml是否可以去除掉這一部分的配置饭望!
root-context.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-4.3.xsd">
<bean id="accountAccessImpl" class="me.laiyijie.demo.dataaccess.AccountAccessImpl"></bean>
<bean id="loginLogAccessImpl" class="me.laiyijie.demo.dataaccess.LoginLogAccessImpl"></bean>
<context:component-scan base-package="me.laiyijie.demo.service"></context:component-scan>
</beans>
yes仗哨!你沒看錯(cuò),配置me.laiyijie.demo.service.UserServiceImpl
這個(gè)bean的代碼被兩個(gè)注解代替了铅辞,而新增了一行:
<context:component-scan base-package="me.laiyijie.demo.service"></context:component-scan>
從文字直譯就可以看出厌漂,這一行代表了
- 從
me.laiyijie.demo.service
包下面掃描componet
而@Service正是componet
中的一種
進(jìn)一步簡(jiǎn)化
是否可以更進(jìn)一步簡(jiǎn)化root-context.xml的配置?巷挥!因?yàn)檫€有兩個(gè)bean是用xml配置的W选!
當(dāng)然可以,通過如下步驟
刪除root-context.xml中的兩行bean配置
在AccountAccessImpl.java和LoginLogAccess.java的類定義前增加@Service注解
-
修改root-context.xml 中componet-scan的 base-package屬性為
me.laiyijie.demo
雏节,如下所示<context:component-scan base-package="me.laiyijie.demo"></context:component-scan>
小結(jié):
- @Service可以將一個(gè)類定義成一個(gè)bean(也就是實(shí)例化并放入工廠)
- @Autowired 可以根據(jù)屬性的類型來注入工廠中存在的該類型的實(shí)例
- 要使用注解胜嗓,需要在ApplicationContext中增加
<context:component-scan base-package="me.laiyijie.demo"></context:component-scan>
配置