spring(三)

一、基于xml自动装配

        自动装配:根据指定的策略,在IOC容器中匹配某一个bean,自动为指定的bean中所依赖的类类型或接口类型属性赋值。

1.场景模拟

//创建类UserController
package com.spring.autowire.controller
public class UserController {

    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void saveUser(){
        userService.saveUser();
    }

}
//创建接口UserService
package com.spring.autowire.service
public interface UserService {

    void saveUser();

}
//创建类UserServiceImpl实现接口UserService
package com.spring.autowire.service.impl
public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void saveUser() {
        userDao.saveUser();
    }

}
//创建接口UserDao
package com.spring.autowire.dao
public interface UserDao {

    void saveUser();

}
//创建类UserDaoImpl实现接口UserDao
package com.spring.autowire.dao.impl
public class UserDaoImpl implements UserDao {

    @Override
    public void saveUser() {
        System.out.println("保存成功");
    }

}

2.配置bean

使用bean标签的autowire属性设置自动装配效果

  • 自动装配方式:byType
  • byType:根据类型匹配IOC容器中某个兼容类型的bean,为属性自动赋值
  • 若在IOC中,没有任何一个兼容类型的bean能够为属性赋值,则该属性不装配,即默认值为null
  • 若在IOC中,有多个兼容类型的bean能为属性赋值,则抛出异常
<bean id="userController" class="com.spring.autowire.controller.UserController" autowire="byType"></bean>
<bean id="userService" class="com.spring.autowire.service.impl.UserServiceImpl" autowire="byType"></bean>
<bean id="userDao" class="com.spring.autowire.dao.impl.UserDaoImpl"></bean>
  • 自动装配方式:byName
  • byName:将自动装配的属性名,作为bean的id在IOC容器中匹配相对应的bean进行赋值
<bean id="userController" class="com.spring.autowire.controller.UserController" autowire="byName"></bean>

<bean id="userService" class="com.spring.autowire.service.impl.UserServiceImpl" autowire="byName"></bean>
<bean id="userServiceImpl" class="com.spring.autowire.service.impl.UserServiceImpl" autowire="byName"></bean>

<bean id="userDao" class="com.spring.autowire.dao.impl.UserDaoImpl"></bean>
<bean id="userDaoImpl" class="com.spring.autowire.dao.impl.UserDaoImpl"></bean>

3.测试

@Test
public void testAutoWireByXML(){
    ApplicationContext ac = new ClassPathXmlApplicationContext("autowire-xml.xml");
    UserController userController = ac.getBean(UserController.class);
    userController.saveUser();
}

二、基于注解管理Bean

Spring通过注解实现自动装配的步骤如下:

  • 引入依赖
  • 开启组件扫描
  • 使用注解定义Bean
  • 依赖注入

1.添加依赖:

//添加依赖
<dependencies>
    <!--spring context依赖-->
    <!--当你引入Spring Context依赖之后,表示将Spring的基础依赖引入了-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>6.0.3</version>
    </dependency>

    <!--junit5测试-->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
    </dependency>

    <!--log4j2的依赖-->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.19.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j2-impl</artifactId>
        <version>2.19.0</version>
    </dependency>
</dependencies>

2.开启组件扫描:

        Spring默认不使用注解装配Bean,因此我们需要在Spring的XML配置中,通过<context:component-scan>元素开启Spring Beans的自动扫描功能。开启此功能后,Spring会自动从扫描指定的包(base-package属性设置)及其子包下的所有类,如果类上使用了@Component注解,就将该类装配到容器中。

//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.xsd">
    <!--开启组件扫描功能-->
    <context:component-scan base-package="com.spring"></context:component-scan>
</beans>

        注意:在使用<context:component-scan>元素开启自动扫描功能前,首先需要在XML配置的一级标签<beans>中添加context相关的约束。

        情况一:最基本的扫描方式

<context:component-scan base-package="com.spring">
</context:component-scan>

          情况二:指定要排除的组件

<context:component-scan base-package="com.spring">
    <!-- context:exclude-filter标签:指定排除规则 -->
    <!-- 
 		type:设置排除或包含的依据
		type="annotation",根据注解排除,expression中设置要排除的注解的全类名
		type="assignable",根据类型排除,expression中设置要排除的类型的全类名
	-->
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <!--<context:exclude-filter type="assignable" expression="com.spring.controller.UserController"/>-->
</context:component-scan>

        情况三:只扫描指定组件

<context:component-scan base-package="com" use-default-filters="false">
    <!-- context:include-filter标签:指定在原有扫描规则的基础上追加的规则 -->
    <!-- use-default-filters属性:取值false表示关闭默认扫描规则 -->
    <!-- 此时必须设置use-default-filters="false",因为默认规则即扫描指定包下所有类 -->
    <!-- 
 		type:设置排除或包含的依据
		type="annotation",根据注解排除,expression中设置要排除的注解的全类名
		type="assignable",根据类型排除,expression中设置要排除的类型的全类名
	-->
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	<!--<context:include-filter type="assignable" expression="com.spring.controller.UserController"/>-->
</context:component-scan>

3.使用注解定义bean

        Spring提供了以下多个注解,这些注解可以直接标注在Java类上,将它们定义成Spring Bean。

注解说明
@Component该注解用于描述Spring中的Bean,它是一个泛化的概念,仅仅表示容器中的一个组件(Bean),并且可以作用在应用的任何层次,如Service层,Dao层等。使用时只需将该注解标注在相应类上即可。
@Repository该注解用于将数据访问层(Dao层)的类标识为Spring中的Bean,其功能与@Component相同
@Service该注解通常用在业务层(Service层),用于将业务层的类标识为Spring中的Bean,其功能与@Component相同
@Controller该注解通常作用在控制层(如SpringMVC 的 Controller),用于将控制层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。

4.@Autowired注入

        单独使用@Autowired注解,默认根据类型装配(默认是byType)

场景一:属性注入

//创建UserDao接口

package com.atguigu.spring6.dao;

public interface UserDao {

    public void print();
}
//创建UserDaoImpl实现

package com.atguigu.spring6.dao.impl;

import com.atguigu.spring6.dao.UserDao;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao {

    @Override
    public void print() {
        System.out.println("Dao层执行结束");
    }
}
//创建UserService接口

package com.atguigu.spring6.service;

public interface UserService {

    public void out();
}
//创建UserServiceImpl实现类

package com.spring.service.impl;

import com.spring.dao.UserDao;
import com.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public void out() {
        userDao.print();
        System.out.println("Service层执行结束");
    }
}
//创建UserController类

package com.spring.controller;

import com.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    public void out() {
        userService.out();
        System.out.println("Controller层执行结束。");
    }

}
//测试一
package com.spring.bean;

import com.spring.controller.UserController;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserTest {
    private Logger logger = LoggerFactory.getLogger(UserTest.class);
    @Test
    public void testAnnotation(){
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        UserController userController = context.getBean("userController", UserController.class);
        userController.out();
        logger.info("执行成功");
    }
}

场景二:set注入

//修改UserServiceImpl类

package com.spring.service.impl;

import com.spring.dao.UserDao;
import com.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    private UserDao userDao;

    @Autowired
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void out() {
        userDao.print();
        System.out.println("Service层执行结束");
    }
}
package com.spring.controller;

import com.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class UserController {

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void out() {
        userService.out();
        System.out.println("Controller层执行结束。");
    }
}

场景三:构造方法注入

//修改UserServiceImpl类

package com.spring.service.impl;

import com.spring.dao.UserDao;
import com.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    private UserDao userDao;

    @Autowired
    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void out() {
        userDao.print();
        System.out.println("Service层执行结束");
    }
}
//修改UserController类

package com.spring.controller;

import com.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class UserController {

    private UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    public void out() {
        userService.out();
        System.out.println("Controller层执行结束。");
    }

}

场景四:形参上注入

//修改UserServiceImpl类

package com.spring.service.impl;

import com.spring.dao.UserDao;
import com.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public UserServiceImpl(@Autowired UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void out() {
        userDao.print();
        System.out.println("Service层执行结束");
    }
}
//修改UserController类

package com.spring.controller;

import com.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class UserController {

    private UserService userService;

    public UserController(@Autowired UserService userService) {
        this.userService = userService;
    }

    public void out() {
        userService.out();
        System.out.println("Controller层执行结束。");
    }

}

场景五:只有一个构造函数,无注解

//修改UserServiceImpl类


package com.spring.service.impl;

import com.spring.dao.UserDao;
import com.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void out() {
        userDao.print();
        System.out.println("Service层执行结束");
    }
}

当有参数的构造方法只有一个时,@Autowired注解可以省略。

场景六:@Auwired注解和@Qualifier注解联合

//添加dao层实现


package com.spring.dao.impl;

import com.spring.dao.UserDao;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoRedisImpl implements UserDao {

    @Override
    public void print() {
        System.out.println("Redis Dao层执行结束");
    }
}

当有多个类实现UserDao时,错误信息提示:不能装配,则这时需要根据名称进行装配。

//修改UserServiceImpl类


package com.spring.service.impl;

import com.spring.dao.UserDao;
import com.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    @Qualifier("userDaoImpl") // 指定bean的名字
    private UserDao userDao;

    @Override
    public void out() {
        userDao.print();
        System.out.println("Service层执行结束");
    }
}

        总结:

  • @Autowired注解可以出现在:属性上,构造方法上,构造方法的参数上,setter方法上。
  • @Autowired注解默认根据类型输入,如果要根据名称输入的话,需要配合@Qualifier注解一起使用。
  • 当带参数的构造方法只有一个,@Autowired注解可以省略。

三、@Resource注入

@Resouece注解也可以实现属性注入,它和@Autowired注解的区别如下:

  • @Resource注解是JDK扩展包中的,即属于JDK的一部分。所以该注解是标准注解,更加具有通用性。
  • @Autowired注解是Spring框架自己的
  • @Resource注解默认根据名称装配,未指定name时,使用属性名作为name。通过name找不到的话会自动启动通过类型装配。
  • @Autowired注解默认根据类型装配byType,如果想根据名称装配,需要配合@Qualifier注解一起用。
  • @Resource注解用在属性上、setter方法上。

  • @Autowired注解用在属性上、setter方法上、构造方法上、构造方法参数上。

@Resource注解属于JDK扩展包,所以不在JDK当中,需要额外引入以下依赖:

<dependency>
    <groupId>jakarta.annotation</groupId>
    <artifactId>jakarta.annotation-api</artifactId>
    <version>2.1.1</version>
</dependency>

场景一:根据name注入

//修改UserDaoImpl类
package com.spring.dao.impl;

import com.spring.dao.UserDao;
import org.springframework.stereotype.Repository;

@Repository("myUserDao")
public class UserDaoImpl implements UserDao {

    @Override
    public void print() {
        System.out.println("Dao层执行结束");
    }
}
//修改UserServiceImpl类

package com.spring.service.impl;

import com.spring.dao.UserDao;
import com.spring.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Resource(name = "myUserDao")
    private UserDao myUserDao;

    @Override
    public void out() {
        myUserDao.print();
        System.out.println("Service层执行结束");
    }
}

场景二:name未知注入

//修改UserDaoImpl类


package com.spring.dao.impl;

import com.spring.dao.UserDao;
import org.springframework.stereotype.Repository;

@Repository("myUserDao")
public class UserDaoImpl implements UserDao {

    @Override
    public void print() {
        System.out.println("Dao层执行结束");
    }
}
//修改UserServiceImpl类

package com.spring.service.impl;

import com.spring.dao.UserDao;
import com.spring.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao myUserDao;

    @Override
    public void out() {
        myUserDao.print();
        System.out.println("Service层执行结束");
    }
}

当@Resource注解使用时没有指定name的时候,还是根据name进行查找,这个name是属性名。

四、全注解开发

全注解开发就是不再使用spring配置文件了,写一个配置类来代替配置文件。

package com.spring.config;

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

@Configuration
//@ComponentScan({"com.spring.controller", "com.spring.service","com.spring.dao"})
@ComponentScan("com.spring")
public class SpringConfig {
}

测试类

@Test
public void testAllAnnotation(){
    ApplicationContext context = new AnnotationConfigApplicationContext(Spring6Config.class);
    UserController userController = context.getBean("userController", UserController.class);
    userController.out();
    logger.info("执行成功");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值