Spring5_01 配置和基于注解的IOC操作

目录

一、Spring的配置

1.xml配置文件

2.进行测试

二、IOC操作Bean管理(基于注解)

1、什么是注解

2、Spring针对Bean管理中创建对象提供注解

3、基于注解方式实现对象创建

第一步 引入依赖

第二步 开启组件扫描

第三步 创建类,在类上面添加创建对象注解

5、基于注解方式实现属性注入

6、完全注解开发--不使用配置文件

(1)创建配置类,代替xml配置文件


一、Spring的配置

1.创建Spring5的xml配置文

​​​​​​​

1.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--    配置User对象创建-->
    <bean id="user" class="com.atguigu.spring5.User"></bean>
</beans>

2.进行测试

@Test
    public void testAdd(){
        //1.加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        //2.获取配置创建的对象
        //user参数为xml中配置的别名
        User user = context.getBean("user", User.class);
        System.out.println(user);
    }


二、IOC操作Bean管理(基于注解)

1、什么是注解

        (1)注解是代码特殊标记符,格式:@注解名称(属性名称=属性值,属性名称=属性值..)

        (2)使用注解,注解作用在类上面,方法上面,属性上面

        (3)使用注解的目的:简化xml配置

2、Spring针对Bean管理中创建对象提供注解

        (1)@Component

        (2)@Service

        (3)@Controller

        (4)@Repository

        *上面四个注解功能时一样的,都可以用来创建Bean对象

3、基于注解方式实现对象创建

第一步 引入依赖

第二步 开启组件扫描

<!--开启组件扫描
        1.如果扫描多个包用逗号隔开
        2.扫描包上层目录
    -->
    <context:component-scan base-package="com.atguigu"></context:component-scan>

第三步 创建类,在类上面添加创建对象注解

以下为测试:

package com.atguigu.spring5.service;

import org.springframework.stereotype.Component;

//在注解里面value属性值可以省略不写
//默认值是类名首字母小写
@Component(value = "userService") //<bean id="userService" class="..."></bean>
public class UserService {

    public void add(){
        System.out.println("service add.....");
    }
}
package com.atguigu.spring5.testdemo;

import com.atguigu.spring5.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring5Demo1 {
    @Test
    public void testUserService(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userService", UserService.class);
        System.out.println(userService);
        userService.add();
    }
}

测试结果:

 补充:关于组件扫描xml文件中两种常见配置

<!--    示例一
        use-default-filters="false" 表示现在不使用默认filter,自己配置filter
        context:include-filter,设置扫描哪些内容
        以下:扫描带Controller的注解类
        -->
    <context:component-scan base-package="com.atguigu" use-default-filters="false">
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--    示例二
        下面配置扫描包所有内容
        context:exclude-filter
        以下:不扫描带Controller的注解类
        -->
    <context:component-scan base-package="com.atguigu">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

5、基于注解方式实现属性注入

(1)@AutoWired:根据属性类型进行自动注入

第一步 把service和dao对象创建,在service和dao类添加创建对象注解

第二步 在service注入dao对象,在service类添加dao类型属性,在属性上面使用注解

package com.atguigu.spring5.service;
import com.atguigu.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//在注解里面value属性值可以省略不写
//默认值是类名首字母小写
@Service(value = "userService") //<bean id="userService" class="..."></bean>
public class UserService {
    //定义dao类型属性,不需要写set方法
    @Autowired//根据类型进行注入
    private UserDao userDao;

    public void add(){
        System.out.println("service add.....");
        userDao.add();
    }
}

(2)@Qualifier:根据名称进行注入

当一个接口有多个实现类时,用@AutoWired无法精准定位

package com.atguigu.spring5.service;
import com.atguigu.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

//在注解里面value属性值可以省略不写
//默认值是类名首字母小写
@Service(value = "userService") //<bean id="userService" class="..."></bean>
public class UserService {
    //定义dao类型属性,不需要写set方法
    @Autowired
    @Qualifier(value = "userDaoImpl1")
    private UserDao userDao;

    public void add(){
        System.out.println("service add.....");
        userDao.add();
    }
}

这个注解的使用,和上面@AutoWired一起使用

(3)@Resource:可以根据类型注入,可以根据名称注入

*如果没有@Resource注解,在头部加入

import javax.annotation.Resource;代码,自动下载依赖的jar包

原因是此注解在jdk版本过底或者过高的版本中没有,此注解是javax扩展包中,并不属于spring

package com.atguigu.spring5.service;
import com.atguigu.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

//在注解里面value属性值可以省略不写
//默认值是类名首字母小写
@Service(value = "userService") //<bean id="userService" class="..."></bean>
public class UserService {
    //定义dao类型属性,不需要写set方法
//    @Autowired
//    @Qualifier(value = "userDaoImpl1")
//    private UserDao userDao;

    @Resource(name = "userDaoImpl1")
    private UserDao userDao;

    public void add(){
        System.out.println("service add.....");
        userDao.add();
    }
}

(4)@Value:注入普通类型属性

package com.atguigu.spring5.service;
import com.atguigu.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

//在注解里面value属性值可以省略不写
//默认值是类名首字母小写
@Service(value = "userService") //<bean id="userService" class="..."></bean>
public class UserService {
    @Value(value = "abc")
    private String name;

    //定义dao类型属性,不需要写set方法
//    @Autowired
//    @Qualifier(value = "userDaoImpl1")
//    private UserDao userDao;

    @Resource(name = "userDaoImpl1")
    private UserDao userDao;

    public void add(){
        System.out.println("service add....."+name);
        userDao.add();
    }
}

*前三个为对于对象类型属性

6、完全注解开发--不使用配置文件

(1)创建配置类,代替xml配置文件

package com.atguigu.spring5.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration //作为配置类
@ComponentScan(basePackages = {"com.atguigu"})
public class SpringConfig {

}

测试类如下: 

@Test
    public void testSpringConfig(){
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();

    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值