由xml配置到純注解的方式是什么

本篇內(nèi)容主要講解“由xml配置到純注解的方式是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“由xml配置到純注解的方式是什么”吧!

成都創(chuàng)新互聯(lián)主營鹽津網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP開發(fā),鹽津h5成都微信小程序搭建,鹽津網(wǎng)站營銷推廣歡迎鹽津等地區(qū)企業(yè)咨詢

一,采用純XML配置文件

在spring 2.5前,使用XML配置文件,這種方式比較簡單,直觀,但配置信息會(huì)非常多,

例如:

<?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:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 包掃描 -->
    <context:component-scan base-package="com.XXX.XX">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>
    <!-- aop支持 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <mvc:annotation-driven/>
    <!-- 注解說明 -->
    <mvc:interceptors>
        <bean class="com.XXX.XX.admin.interceptor.AdminAuthInterceptor"/>
    </mvc:interceptors>
    <!--上傳文件所需要的bean-->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
          p:defaultEncoding="utf-8"/>
    <!-- 配置數(shù)據(jù)源 -->
    <bean id="baseDataBase" class="com.alibaba.druid.pool.DruidDataSource"
          destroy-method="close">
        <property name="url" value="${druid.url}"/>
        <property name="username" value="${druid.username}"/>
        <property name="password" value="${druid.password}"/>
        <property name="driverClassName" value="${druid.driverClassName}"/>
        <property name="filters" value="${druid.filters}"/>
    </bean>
    <bean id="user" class="com.XXX.XX.entity.User">
        <property name="id" value="1" />
        <property name="name" value="zhangsan" />
    </bean>
</beans>

二,XML+ 注解的方式

在Spring 2.5以后,加入了注解,主要是SSM框架

主要是用@開頭進(jìn)行標(biāo)注, 最重要的 @component,查看源碼,

/** Target里面放數(shù)組,ElementType使用的范圍,
    TYPE,  類,接口,注解,enum
    FIELD,  用于描述域
    METHOD, 方法上
    PARAMETER,  參數(shù)
    CONSTRUCTOR,  構(gòu)成器
    LOCAL_VARIABLE, 局部變量
    ANNOTATION_TYPE,
    PACKAGE,  包名
 */
@Target({ElementType.TYPE})
/** Retention 聲明在什么時(shí)候生效
    SOURCE,  只保留在源文件,.java
    CLASS,   保留在編譯后的 class文件
    RUNTIME  .java .class 都包含
**/
@Retention(RetentionPolicy.RUNTIME)
//注解標(biāo)記元素的注解信息包含在javadoc中
@Documented
public @interface Component {
    String value() default "";
}

在開發(fā)的過程中, 目錄結(jié)構(gòu)會(huì)新建controller,service,dao三層結(jié)構(gòu),對(duì)應(yīng)@Controller,@Service,@Repository三個(gè)注解。 其實(shí)點(diǎn)擊源碼,發(fā)現(xiàn)這三個(gè)注解底層也是用的@Component注解

1. 新建項(xiàng)目,在resource 目錄下新建spring.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-3.0.xsd
	 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 包掃描 根據(jù)類型包含或者不包含,type=annotation , aspectj,assignable  -->
    <context:component-scan base-package="com.study"  >
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
    <!--<bean id="userController" class="com.study.controller.UserController"/>-->
</beans>

2,新建路徑com.study.admin.controller 下的類 UserController, 添加上面四種注解中的一種

3,添加測(cè)試類 UserTest,都能打印結(jié)果,說明類已經(jīng)加載IOC容器中,

public class UserTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring.xml");
        //使用類型可以獲取到bean
        UserController bean = ioc.getBean(UserController.class);
        System.out.println(bean);
    }
    @Test
    public void test1() {
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring.xml");
        //根據(jù)名稱,首字母小寫,也可以獲取bean
        UserController bean = (UserController) ioc.getBean("userController");
        System.out.println(bean);
    }
}

4,新建service接口 com.study.admin.service.BaseService  和實(shí)現(xiàn)類 com.study.admin.service.impl.UserImpl, dao層數(shù)據(jù)類  com.study.admin.dao.UserDao。

在controller層,通過@Autowired 調(diào)用service實(shí)現(xiàn)業(yè)務(wù)邏輯,這里的Autowired首先通過類型匹配,其次通過名字匹配,

4.1,注入的時(shí)候是接口,那么會(huì)自動(dòng)找到實(shí)現(xiàn)類,但是如果實(shí)現(xiàn)類有多個(gè),這里就會(huì)報(bào)錯(cuò),

controller 層
@Controller
public class UserController {

    /**
     * 1,如果BaseService只有一個(gè)實(shí)現(xiàn)類,是正常
     * 2,如果有兩個(gè)實(shí)現(xiàn)類,的解決方案
     *   2.1,后面申明的名稱,換成對(duì)應(yīng)的類名稱就可以( @Autowired BaseService userService;)
     *   2.2, 類注入service 加上對(duì)應(yīng)名稱 @Service("baseService"), 
     *   2.3,@Autowired 下面再加注解 @Qualifier("userService")
     *   2.4  在其中一個(gè)實(shí)現(xiàn)類加上 @Primary
     *
     *   Resource 和 Autowired 的區(qū)別
     *   Autowired spring里面的包,優(yōu)先類型匹配,然后再名稱匹配
     *   Resource 是JDK的包,優(yōu)先名字匹配,然后類型匹配
     */
    @Autowired
    BaseService baseService;

    public void test() {
        baseService.getDate();
    }
}

//service 層
//接口
public interface BaseService {

    void getDate();
}
//實(shí)現(xiàn)類
@Service
public class RoleService implements BaseService {
    @Override
    public void getDate() {
        System.out.println("roleService get data");
    }
}

@Service
public class UserService implements BaseService {

    @Override
    public void getDate() {
        System.out.println("userService get data");
    }
}

三,純注解的方式,Javaconfig

從spring 3.0 開始,基本使用spring boot 來開發(fā),無xml配置文件

1. 使用@configuration 注解代替XML,  @ComponentScan代替包掃描,

2,@Bean 用來申明外部Bean,需要自己new然后返回,方法名就是IOC中存的名字

3.使用 @PropertySource("db.properties") 映入外部配置文件,@Value("${MySQL.name}") 獲取值,${} 根據(jù)名稱獲取值,#{} 獲取對(duì)象的屬性值

到此,相信大家對(duì)“由xml配置到純注解的方式是什么”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

文章名稱:由xml配置到純注解的方式是什么
鏈接分享:http://muchs.cn/article4/ijddie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、移動(dòng)網(wǎng)站建設(shè)網(wǎng)站內(nèi)鏈、定制開發(fā)外貿(mào)網(wǎng)站建設(shè)、App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)