Spring基于xml和注解的配置

1)基于xml的配置

        编码流程

        1.创建需要被Spring管理的类

        2.创建xml配置文件,使用bean标签配置对象

控制层

        public class HelloController{ }

业务层

        public class HelloSErverice{ }

持久层

        public class HelloDao{ }

实体类

        public class Student{ }

配置文件:bean.xml

version="1.0" encoding="UTF-8"?>

xmlns="http://www.springframework.org/schema/beans"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      xsi:schemaLocation="http://www.springframework.org/schema/beans

       https://www.springframework.org/schema/beans/spring-beans.xsd">

        id="helloController" class="com.soft.controller.HelloController"/>

    id="helloService" class="com.soft.service.HelloService"/>

    id="helloDao" class="com.soft.dao.HelloDao"/>

    id="student" class="com.soft.entity.Student"/>

创建一个测试类来测试以下基于xml配置的是否成功

        public class SpringIoTest{

                @Test

                public void testBean(){

                /*

        ClassPath:代码解析后的文件路径

        Xml:文件类型

        ApplicationContext:应用的上下文环境

*/

//根据配置文件获取上下文环境

        ApplicationContext context =new ClasspathXmlApplicationContext("/bean.xml")

       //根据上下文获取对象

        HelloController helloController =context.getBean("helloController“,HelloController.class);

        HelloDao hellodao =context.getBean("helloDao",HelloDao.class);

        Student student=context.getBean("student",Student.class);

}

}

基于注解的配置

        1.创建需要被Spring管理的类,在上面添加注解@Component

        作用是代替xml的配置(<bean id="id名" Class="类路径">)

        其中,id默认是类名首字母小写,也可以通过注解value属性修改,但是需要注意修改名字时

必须只有一个属性,并且该属性的值为value,value可以省略不写 改名字:@Component("名字")。改名字:@Component(value="名字")

        根据分层思想@Component注解可以拆分为三个注解。控制层:@Controller;业务层:@Service;持久层: @Repository;这里的三个注解就可以对应三个结构的注解,直接标记哪个层,代替了@Component,甚至比它多加了一个标记的作用。

2. 创建xml文件,添加注解配置 -scan base-package="com.soft"/> 。

控制层

@Controller

public class HelloController{}

业务层

@Service

public class HelloService{}

持久层

@Repository

public class HelloDao{}

实体类

@Component

public class Student{}

version="1.0" encoding="UTF-8"?>

测试

3)基于配置类配置

Spring 3.x 之后推出配置类编码方式,用于取代 xml,使用全注解的形式开发。同时也为 SpringBoot 无配置文件自动配置打下基础。

编码流程

1. 创建需要被 Spring 管理的类。

2. 创建配置类,添加 @Configuration 注解。

@Configuration 注解标记配置类,同时该注解继承了 @Component 注解,也具备了创建对 象的功能,毕竟配置类也是需要实例才可以执行。

3. 配置类中编写带返回值的方法,方法的返回值为需要被 Spring 管理的类型。

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

       https://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/context

       https://www.springframework.org/schema/context/spring-context.xsd">

   

   

    base-package="com.soft"/>

这里只需要配置一个开启注解的就行,扫描注解所在的父类包即可

}

测试

public class SpringIocTest {    @Test

   public void testBean(){        /*

           ClassPath:代码解析后的文件路径

           Xml:文件类型

           ApplicationContext:应用的上下文环境

       */

       // 根据配置文件获取上下文环境

       ApplicationContext context = new

ClassPathXmlApplicationContext("/bean.xml");        // 根据上下文获取对象

       HelloController helloController = context.getBean("helloController",

HelloController.class);        HelloService helloService = context.getBean("helloService",

HelloService.class);        HelloDao helloDao = context.getBean("helloDao", HelloDao.class);        Student student = context.getBean("student", Student.class);   } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值