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

发布时间:2021-10-11 21:37:59 作者:iii
来源:亿速云 阅读:115

本篇内容主要讲解“由xml配置到纯注解的方式是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“由xml配置到纯注解的方式是什么”吧!

一,采用纯XML配置文件

在spring 2.5前,使用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: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"/>
    <!-- 配置数据源 -->
    <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框架

主要是用@开头进行标注, 最重要的 @component,查看源码,

/** Target里面放数组,ElementType使用的范围,
    TYPE,  类,接口,注解,enum
    FIELD,  用于描述域
    METHOD, 方法上
    PARAMETER,  参数
    CONSTRUCTOR,  构成器
    LOCAL_VARIABLE, 局部变量
    ANNOTATION_TYPE,
    PACKAGE,  包名
 */
@Target({ElementType.TYPE})
/** Retention 声明在什么时候生效
    SOURCE,  只保留在源文件,.java
    CLASS,   保留在编译后的 class文件
    RUNTIME  .java .class 都包含
**/
@Retention(RetentionPolicy.RUNTIME)
//注解标记元素的注解信息包含在javadoc中
@Documented
public @interface Component {
    String value() default "";
}

在开发的过程中, 目录结构会新建controller,service,dao三层结构,对应@Controller,@Service,@Repository三个注解。 其实点击源码,发现这三个注解底层也是用的@Component注解

1. 新建项目,在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">

    <!-- 包扫描 根据类型包含或者不包含,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,添加测试类 UserTest,都能打印结果,说明类已经加载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");
        //根据名称,首字母小写,也可以获取bean
        UserController bean = (UserController) ioc.getBean("userController");
        System.out.println(bean);
    }
}

4,新建service接口 com.study.admin.service.BaseService  和实现类 com.study.admin.service.impl.UserImpl, dao层数据类  com.study.admin.dao.UserDao。

在controller层,通过@Autowired 调用service实现业务逻辑,这里的Autowired首先通过类型匹配,其次通过名字匹配,

4.1,注入的时候是接口,那么会自动找到实现类,但是如果实现类有多个,这里就会报错,

controller 层
@Controller
public class UserController {

    /**
     * 1,如果BaseService只有一个实现类,是正常
     * 2,如果有两个实现类,的解决方案
     *   2.1,后面申明的名称,换成对应的类名称就可以( @Autowired BaseService userService;)
     *   2.2, 类注入service 加上对应名称 @Service("baseService"), 
     *   2.3,@Autowired 下面再加注解 @Qualifier("userService")
     *   2.4  在其中一个实现类加上 @Primary
     *
     *   Resource 和 Autowired 的区别
     *   Autowired spring里面的包,优先类型匹配,然后再名称匹配
     *   Resource 是JDK的包,优先名字匹配,然后类型匹配
     */
    @Autowired
    BaseService baseService;

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

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

    void getDate();
}
//实现类
@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 来开发,无xml配置文件

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

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

3.使用 @PropertySource("db.properties") 映入外部配置文件,@Value("${mysql.name}") 获取值,${} 根据名称获取值,#{} 获取对象的属性值

到此,相信大家对“由xml配置到纯注解的方式是什么”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. xml配置的优缺点以及和Spring注解的区别
  2. 3、SpringMVC基于XML配置和基于注解的配置

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring mysql

上一篇:如何理解Redis分布式锁

下一篇:快速地将事件发送到Google Analytics的方法步骤

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》