spring学习笔记(第二部分)

简介: 自学笔记

2.2.9、实验九:p命名空间

引入p命名空间后,可以通过以下方式为bean的各个属性赋值

<bean id="studentSix" class="com.atguigu.spring.bean.Student"
p:id="1006" p:name="小明" p:clazz-ref="clazzOne" p:teacherMapref="teacherMap"></bean>

2.2.10、实验十:引入外部属性文件

①加入依赖

<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!-- 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>

②创建spring配置文件

image-20220930175717786

③配置bean

<!--声明数据源DataSource,作用是连接数据库-->
    <bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        <!--set注入给DruidDataSource提供连接数据库信息-->
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/自己的数据库名?serverTimezone=UTC&amp;
                  useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false" />
        <property name="username" value="用户名" />
        <property name="password" value="自己的密码"/>
        <property name="maxActive" value="20" />
    </bean>

④测试

@Test
    public void testDataSource() throws SQLException {
   
   
        ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-datasource.xml");
        DruidDataSource dataSource = ioc.getBean(DruidDataSource.class);
        System.out.println(dataSource.getConnection());
    }

2.2.11、实验十一:bean的作用域

①概念

在Spring中可以通过配置bean标签的scope属性来指定bean的作用域范围,各取值含义参加下表:

取值 含义 创建对象的时机
singleton(默认) 在IOC容器中,这个bean的对象始终为单实例 IOC容器初始化时
prototype 这个bean在IOC容器中有多个实例 获取bean时

如果是在WebApplicationContext环境下还会有另外两个作用域(但不常用):

取值 含义
request 在一个请求范围内有效
session 在一个会话范围内有效

②创建类User

public class User {
   
   
private Integer id;
private String username;
private String password;
private Integer age;
public User() {
   
   
}
public User(Integer id, String username, String password, Integer age) {
   
   
            this.id = id;
            this.username = username;
            this.password = password;
            this.age = age;
            }
            public Integer getId() {
   
   
            return id;
            }
            public void setId(Integer id) {
   
   
            this.id = id;
            }
            public String getUsername() {
   
   
            return username;
            }
            public void setUsername(String username) {
   
   
            this.username = username;
            }
            public String getPassword() {
   
   
            return password;
            }
            public void setPassword(String password) {
   
   
            this.password = password;
            }
            public Integer getAge() {
   
   
            return age;
            }
            public void setAge(Integer age) {
   
   
            this.age = age;
}
            @Override
            public String toString() {
   
   
            return "User{" +
            "id=" + id +
            ", username='" + username + '\'' +
            ", password='" + password + '\'' +
            ", age=" + age +
            '}';
            }
}

③配置bean

<!-- scope属性:取值singleton(默认值),bean在IOC容器中只有一个实例,IOC容器初始化时创建
对象 -->
<!-- scope属性:取值prototype,bean在IOC容器中可以有多个实例,getBean()时创建对象 -->
<bean class="com.atguigu.bean.User" scope="prototype"></bean>

④测试

@Test
public void testBeanScope(){
   
   
        ApplicationContext ac = new ClassPathXmlApplicationContext("springscope.xml");
        User user1 = ac.getBean(User.class);
        User user2 = ac.getBean(User.class);
        System.out.println(user1==user2);
}

2.2.12、实验十二:bean的生命周期

①具体的生命周期过程

  • bean对象创建(调用无参构造器)
  • 给bean对象设置属性
  • bean对象初始化之前操作(由bean的后置处理器负责)
  • bean对象初始化(需在配置bean时指定初始化方法)
  • bean对象初始化之后操作(由bean的后置处理器负责)
  • bean对象就绪可以使用
  • bean对象销毁(需在配置bean时指定销毁方法)
  • IOC容器关闭

②修改类User

public class User {
   
   
    private Integer id;
    private String username;
    private String password;
    private Integer age;

    public User(Integer id, String username, String password, Integer age) {
   
   
        this.id = id;
        this.username = username;
        this.password = password;
        this.age = age;
    }

    public User() {
   
   
        System.out.println("生命周期1:实例化");
    }

    public Integer getId() {
   
   
        return id;
    }

    public void setId(Integer id) {
   
   
        System.out.println("生命周期2:依赖注入");
        this.id = id;
    }

    public String getUsername() {
   
   
        return username;
    }

    public void setUsername(String username) {
   
   
        this.username = username;
    }

    public String getPassword() {
   
   
        return password;
    }

    public void setPassword(String password) {
   
   
        this.password = password;
    }

    public Integer getAge() {
   
   
        return age;
    }

    public void setAge(Integer age) {
   
   
        this.age = age;
    }

    @Override
    public String toString() {
   
   
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                '}';
    }
    public void initMethod(){
   
   
        System.out.println("生命周期3:初始化");
    }
    public void destroyMethod(){
   
   
        System.out.println("生命周期4:销毁");
    }
}
  • 注意其中的initMethod()和destroyMethod(),可以通过配置bean指定为初始化和销毁的方法

③配置bean

<!-- 使用init-method属性指定初始化方法 -->
<!-- 使用destroy-method属性指定销毁方法 -->
<bean id="user" class="com.xujicheng.spring.pojo.User" init-method="initMethod" destroy-method="destroyMethod">
    <property name="id" value="1"></property>
    <property name="username" value="洛无极"></property>
    <property name="age" value="20"></property>
    <property name="password" value="123456"></property>
</bean>

④测试

public class LifeCycleTest {
   
   
    @Test
    public void testLifeCycle(){
   
   
        //ConfigurableApplicationContext是ApplicationContext的子接口,其中扩展了刷新和关闭容器的方法
        ConfigurableApplicationContext ioc = new ClassPathXmlApplicationContext("spring-lifecycle.xml");
        User user = ioc.getBean(User.class);
        System.out.println(user);
        ioc.close();
    }
}

⑤bean的后置处理器

bean的后置处理器会在生命周期的初始化前后添加额外的操作,需要实现BeanPostProcessor接口, 且配置到IOC容器中,需要注意的是,bean后置处理器不是单独针对某一个bean生效,而是针对IOC容 器中所有bean都会执行

创建bean的后置处理器:

public class MyBeanPostProcessor implements BeanPostProcessor {
   
   
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
   
   
        //此方法在bean的生命周期初始化之前来执行
        System.out.println("MyBeanPostProcessor-->后置处理器postProcessBeforeInitialization");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
   
   
        //此方法在bean的生命周期初始化之后来执行
        System.out.println("MyBeanPostProcessor-->后置处理器postProcessAfterInitialization");
        return bean;
    }
}

在IOC容器中配置后置处理器:

<!-- bean的后置处理器要放入IOC容器才能生效 -->
<bean id="myBeanProcessor" class="com.atguigu.spring.process.MyBeanProcessor"/>
  • 总结
bean的生命周期:
1、实例化
2、依赖注入
3、后置处理器的postProcessBeforeInitialization
4、初始化,需要通过bean的init-method属性指定初始化的方法
5、后置处理器的postProcessAfterInitialization
6、IOC容器关闭时销毁,需要通过bean的destroy-method属性方法指定销毁的方法
注意:
若bean的作用域为单例时,生命周期的前三个步骤会在获取IOC容器时执行
若bean的作用域为多例时,生命周期的前三个步骤会在获取bean时执行

bean的后置处理器会在生命周期的初始化前后添加额外的操作,需要实现BeanPostProcessor接口, 且配置到IOC容器中,需要注意的是,bean后置处理器不是单独针对某一个bean生效,而是针对IOC容 器中所有bean都会执行

2.2.13、实验十三:FactoryBean

①简介

FactoryBean是Spring提供的一种整合第三方框架的常用机制。和普通的bean不同,配置一个 FactoryBean类型的bean,在获取bean的时候得到的并不是class属性中配置的这个类的对象,而是 getObject()方法的返回值。通过这种机制,Spring可以帮我们把复杂组件创建的详细过程和繁琐细节都 屏蔽起来,只把最简洁的使用界面展示给我们。

将来我们整合Mybatis时,Spring就是通过FactoryBean机制来帮我们创建SqlSessionFactory对象的。

/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory;
import org.springframework.lang.Nullable;
/**
* Interface to be implemented by objects used within a {@link BeanFactory}
which
* are themselves factories for individual objects. If a bean implements this
* interface, it is used as a factory for an object to expose, not directly as a
* bean instance that will be exposed itself.
*
* <p><b>NB: A bean that implements this interface cannot be used as a normal
bean.</b>
* A FactoryBean is defined in a bean style, but the object exposed for bean
* references ({@link #getObject()}) is always the object that it creates.
*
* <p>FactoryBeans can support singletons and prototypes, and can either create
* objects lazily on demand or eagerly on startup. The {@link SmartFactoryBean}
* interface allows for exposing more fine-grained behavioral metadata.
*
* <p>This interface is heavily used within the framework itself, for example
for
* the AOP {@link org.springframework.aop.framework.ProxyFactoryBean} or the
* {@link org.springframework.jndi.JndiObjectFactoryBean}. It can be used for
* custom components as well; however, this is only common for infrastructure
code.
*
* <p><b>{@code FactoryBean} is a programmatic contract. Implementations are not
* supposed to rely on annotation-driven injection or other reflective
facilities.</b>
* {@link #getObjectType()} {@link #getObject()} invocations may arrive early in
the
* bootstrap process, even ahead of any post-processor setup. If you need access
to
* other beans, implement {@link BeanFactoryAware} and obtain them
programmatically.
*
* <p><b>The container is only responsible for managing the lifecycle of the
FactoryBean
* instance, not the lifecycle of the objects created by the FactoryBean.</b>
Therefore,
* a destroy method on an exposed bean object (such as {@linkjava.io.Closeable#close()}
* will <i>not</i> be called automatically. Instead, a FactoryBean should
implement
* {@link DisposableBean} and delegate any such close call to the underlying
object.
*
* <p>Finally, FactoryBean objects participate in the containing BeanFactory's
* synchronization of bean creation. There is usually no need for internal
* synchronization other than for purposes of lazy initialization within the
* FactoryBean itself (or the like).
*
* @author Rod Johnson
* @author Juergen Hoeller
* @since 08.03.2003
* @param <T> the bean type
* @see org.springframework.beans.factory.BeanFactory
* @see org.springframework.aop.framework.ProxyFactoryBean
* @see org.springframework.jndi.JndiObjectFactoryBean
*/
public interface FactoryBean<T> {
   
   
/**
* The name of an attribute that can be
* {@link org.springframework.core.AttributeAccessor#setAttribute set} on a
* {@link org.springframework.beans.factory.config.BeanDefinition} so that
* factory beans can signal their object type when it can't be deduced from
* the factory bean class.
* @since 5.2
*/
String OBJECT_TYPE_ATTRIBUTE = "factoryBeanObjectType";
/**
* Return an instance (possibly shared or independent) of the object
* managed by this factory.
* <p>As with a {@link BeanFactory}, this allows support for both the
* Singleton and Prototype design pattern.
* <p>If this FactoryBean is not fully initialized yet at the time of
* the call (for example because it is involved in a circular reference),
* throw a corresponding {@link FactoryBeanNotInitializedException}.
* <p>As of Spring 2.0, FactoryBeans are allowed to return {@code null}
* objects. The factory will consider this as normal value to be used; it
* will not throw a FactoryBeanNotInitializedException in this case anymore.
* FactoryBean implementations are encouraged to throw
* FactoryBeanNotInitializedException themselves now, as appropriate.
* @return an instance of the bean (can be {@code null})
* @throws Exception in case of creation errors
* @see FactoryBeanNotInitializedException
*/
@Nullable
T getObject() throws Exception;
/**
* Return the type of object that this FactoryBean creates,
* or {@code null} if not known in advance.
* <p>This allows one to check for specific types of beans without
* instantiating objects, for example on autowiring.
* <p>In the case of implementations that are creating a singleton object,
* this method should try to avoid singleton creation as far as possible;
* it should rather estimate the type in advance.
* For prototypes, returning a meaningful type here is advisable too.
* <p>This method can be called <i>before</i> this FactoryBean has
* been fully initialized. It must not rely on state created during
* initialization; of course, it can still use such state if available.
* <p><b>NOTE:</b> Autowiring will simply ignore FactoryBeans that return
* {@code null} here. Therefore it is highly recommended to implement
* this method properly, using the current state of the FactoryBean.
* @return the type of object that this FactoryBean creates,
* or {@code null} if not known at the time of the call
* @see ListableBeanFactory#getBeansOfType
*/
@Nullable
Class<?> getObjectType();
/**
* Is the object managed by this factory a singleton? That is,
* will {@link #getObject()} always return the same object
* (a reference that can be cached)?
* <p><b>NOTE:</b> If a FactoryBean indicates to hold a singleton object,
* the object returned from {@code getObject()} might get cached
* by the owning BeanFactory. Hence, do not return {@code true}
* unless the FactoryBean always exposes the same reference.
* <p>The singleton status of the FactoryBean itself will generally
* be provided by the owning BeanFactory; usually, it has to be
* defined as singleton there.
* <p><b>NOTE:</b> This method returning {@code false} does not
* necessarily indicate that returned objects are independent instances.
* An implementation of the extended {@link SmartFactoryBean} interface
* may explicitly indicate independent instances through its
* {@link SmartFactoryBean#isPrototype()} method. Plain {@link FactoryBean}
* implementations which do not implement this extended interface are
* simply assumed to always return independent instances if the
* {@code isSingleton()} implementation returns {@code false}.
* <p>The default implementation returns {@code true}, since a
* {@code FactoryBean} typically manages a singleton instance.
* @return whether the exposed object is a singleton
* @see #getObject()
* @see SmartFactoryBean#isPrototype()
*/
default boolean isSingleton() {
   
   
return true;
}
}

②创建类UserFactoryBean

/**
 * FactoryBean是一个接口,需要创建一个类实现该接口
 * 其中有三个方法:
 * getObject():提供一个对象交给IOC容器管理
 * isSingleton():所提供的对象是否单例
 * 当把FactoryBean的实现类配置为bean时,会将当前类getObject()所返回的对象交给IOC容器管理
 * @auhor xujicheng
 * @since 2022/10/1 16:33
 */
public class UserFactoryBean implements FactoryBean<User> {
   
   
    @Override
    public User getObject() throws Exception {
   
   
        return new User();
    }

    @Override
    public Class<?> getObjectType() {
   
   
        return User.class;
    }
}

③配置bean

<bean id="user" class="com.atguigu.bean.UserFactoryBean"></bean>

④测试

@Test
public void testUserFactoryBean(){
   
   
//获取IOC容器
    ApplicationContext ac = new ClassPathXmlApplicationContext("springfactorybean.xml");
    User user = (User) ac.getBean("user");
    System.out.println(user);
}

2.2.14、实验十四:基于xml的自动装配

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

①场景模拟

创建类UserController

public class UserController {
   
   
    private UserService userService;
    public void setUserService(UserService userService) {
   
   
    this.userService = userService;
    }
    public void saveUser(){
   
   
    userService.saveUser();
    }
}

创建接口UserService

public interface UserService {
   
   
    void saveUser();
}

创建类UserServiceImpl实现接口UserService

public class UserServiceImpl implements UserService {
   
   
private UserDao userDao;
public void setUserDao(UserDao userDao) {
   
   
    this.userDao = userDao;
}
        @Override
        public void saveUser() {
   
   
        userDao.saveUser();
        }
}

创建接口UserDao

public interface UserDao {
   
   
    void saveUser();
}

创建类UserDaoImpl实现接口UserDao

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

②配置bean

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

自动装配方式:byType

byType:根据类型匹配IOC容器中的某个兼容类型的bean,为属性自动赋值

若在IOC中,没有任何一个兼容类型的bean能够为属性赋值,则该属性不装配,即值为默认值 null

若在IOC中,有多个兼容类型的bean能够为属性赋值,则抛出异常 NoUniqueBeanDefinitionException

<bean id="userController"
class="com.atguigu.autowire.xml.controller.UserController" autowire="byType">
</bean>
<bean id="userService"
class="com.atguigu.autowire.xml.service.impl.UserServiceImpl" autowire="byType">
</bean>
<bean id="userDao" class="com.atguigu.autowire.xml.dao.impl.UserDaoImpl"></bean>

自动装配方式:byName

byName:将自动装配的属性的属性名,作为bean的id在IOC容器中匹配相对应的bean进行赋值

<bean id="userController"
class="com.atguigu.autowire.xml.controller.UserController" autowire="byName">
</bean>
<bean id="userService"
class="com.atguigu.autowire.xml.service.impl.UserServiceImpl" autowire="byName">
</bean>
<bean id="userServiceImpl"
class="com.atguigu.autowire.xml.service.impl.UserServiceImpl" autowire="byName">
</bean>
<bean id="userDao" class="com.atguigu.autowire.xml.dao.impl.UserDaoImpl"></bean>
<bean id="userDaoImpl" class="com.atguigu.autowire.xml.dao.impl.UserDaoImpl">
</bean>

③测试

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

自动装配方式的总结

自动装配:
根据指定的策略,在IOC容器中匹配某个bean,自动为bean中的类类型的属性或接口类型的属性赋值
可以通过bean标签中的autowire属性设置自动装配的策略
自动装配的策略:
no,default:表示不装配,即bean中的属性不会自动匹配某个bean为属性赋值,此时属性使用默认值
byType:根据要赋值的属性的类型,在IOC容器中匹配某个bean,为属性赋值

注意:以下两种为特殊情况
1、若通过类型没有找到任何一个类型匹配的bean,测i在不装配,属性使用默认值
2、若通过类型找到了多个类型匹配的bean,此时会抛出异常NoUniqueBeanDefinitionException
总结:当使用byType实现自动装配式,IOC容器中有且只有一个类型匹配的bean能够为属性赋值

byName:将要赋值的属性的属性名作为bean的id在IOC容器中匹配某个bean,为属性赋值
总结:当类型匹配的bean有多个时,此时可以使用byName实现自动装配

2.3、基于注解管理bean

2.3.1、实验一:标记与扫描

①注解

和 XML 配置文件一样,注解本身并不能执行,注解本身仅仅只是做一个标记,具体的功能是框架检测 到注解标记的位置,然后针对这个位置按照注解标记的功能来执行具体操作。

本质上:所有一切的操作都是Java代码来完成的,XML和注解只是告诉框架中的Java代码如何执行。

举例:元旦联欢会要布置教室,蓝色的地方贴上元旦快乐四个字,红色的地方贴上拉花,黄色的地方贴 上气球。

image-20220929124640114

班长做了所有标记,同学们来完成具体工作。墙上的标记相当于我们在代码中使用的注解,后面同学们 做的工作,相当于框架的具体操作。

②扫描

Spring 为了知道程序员在哪些地方标记了什么注解,就需要通过扫描的方式,来进行检测。然后根据注 解进行后续操作。

③新建Maven Module

<dependencies>
<!-- 基于Maven依赖传递性,导入spring-context依赖即可导入当前所需所有jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.1</version>
</dependency>
<!-- junit测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

④创建Spring配置文件

image-20220929124723100

⑤标识组件的常用注解

@Component:将类标识为普通组件

@Controller:将类标识为控制层组件

@Service:将类标 识为业务层组件

@Repository:将类标识为持久层组件

问:以上四个注解有什么关系和区别?

image-20220929124806272

通过查看源码我们得知,@Controller、@Service、@Repository这三个注解只是在@Component注解 的基础上起了三个新的名字。

对于Spring使用IOC容器管理这些组件来说没有区别。所以@Controller、@Service、@Repository这 三个注解只是给开发人员看的,让我们能够便于分辨组件的作用

注意:虽然它们本质上一样,但是为了代码的可读性,为了程序结构严谨我们肯定不能随便胡乱标记。

⑥创建组件

创建控制层组件

@Controller
public class UserController {
   
   
}

创建接口UserService

public interface UserService {
   
   
}

创建业务层组件UserServiceImpl

@Service
public class UserServiceImpl implements UserService {
   
   
}

创建接口UserDao

public interface UserDao {
   
   
}

创建持久层组件UserDaoImpl

@Repository
public class UserDaoImpl implements UserDao {
   
   
}

⑦扫描组件

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

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

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

<context:component-scan base-package="com.atguigu">
<!-- 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.atguigu.controller.UserController"/>-->
</context:component-scan>

情况三:仅扫描指定组件

<context:component-scan base-package="com.atguigu" 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.atguigu.controller.UserController"/>-->
</context:component-scan>

⑧测试

@Test
public void testAutowireByAnnotation(){
   
   
    ApplicationContext ac = new
    ClassPathXmlApplicationContext("applicationContext.xml");
    UserController userController = ac.getBean(UserController.class);
    System.out.println(userController);
    UserService userService = ac.getBean(UserService.class);
    System.out.println(userService);
    UserDao userDao = ac.getBean(UserDao.class);
    System.out.println(userDao);
    }

⑨组件所对应的bean的id

在我们使用XML方式管理bean的时候,每个bean都有一个唯一标识,便于在其他地方引用。现在使用 注解后,每个组件仍然应该有一个唯一标识。

  • 默认情况

    • 类名首字母小写就是bean的id。例如:UserController类对应的bean的id就是userController。 自定义bean的id
    • 可通过标识组件的注解的value属性设置自定义的bean的id
    • @Service("userService")//默认为userServiceImpl public class UserServiceImpl implements UserService {}
  • 扫描的总结

context:exclude-filter:排除扫描
type:设置排除扫描的方式
type:"annotation | assignable"
annotation:根据注解的类型进行排除,expression需要设置排除的注解的全类名
assignable:根据类的类型进行排除,expression需要设置排除的类的全类名

context:include-filter:包含扫描
注意:需要在context:component-scan标签中设置use-default-filters="false"
use-default-filters="true"(默认),所设置的包下所有的类都需要扫描,此时可以使用排除扫描
use-default-filters="false",所设置的报下所有的类都不需要扫描,此时可以使用包含扫描

2.3.2、实验二:基于注解的自动装配

①场景模拟

参考基于xml的自动装配

在UserController中声明UserService对象

在UserServiceImpl中声明UserDao对象

②@Autowired注解

在成员变量上直接标记@Autowired注解即可完成自动装配,不需要提供setXxx()方法。以后我们在项 目中的正式用法就是这样。

@Controller
public class UserController {
   
   
    @Autowired
    private UserService userService;
    public void saveUser(){
   
   
    userService.saveUser();
    }
}
public interface UserService {
   
   
    void saveUser();
}
@Service
public class UserServiceImpl implements UserService {
   
   
    @Autowired
    private UserDao userDao;
    @Override
    public void saveUser() {
   
   
    userDao.saveUser();
    }
}
@Repository
public class UserDaoImpl implements UserDao {
   
   
    @Override
    public void saveUser() {
   
   
    System.out.println("保存成功");
    }
}

③@Autowired注解其他细节

  • @Autowired注解可以标记在构造器和set方法上
@Controller
public class UserController {
   
   
    private UserService userService;
    @Autowired
    public UserController(UserService userService){
   
   
    this.userService = userService;
    }
    public void saveUser(){
   
   
    userService.saveUser();
    }
}
@Controller
public class UserController {
   
   
    private UserService userService;
    @Autowired
    public void setUserService(UserService userService){
   
   
    this.userService = userService;
    }
    public void saveUser(){
   
   
    userService.saveUser();
    }
}

④@Autowired工作流程

image-20220929125338716

  • 首先根据所需要的组件类型到IOC容器中查找
    • 能够找到唯一的bean:直接执行装配
    • 如果完全找不到匹配这个类型的bean:装配失败
    • 和所需类型匹配的bean不止一个
      • 没有@Qualifier注解:根据@Autowired标记位置成员变量的变量名作为bean的id进行 匹配
        • 能够找到:执行装配
        • 找不到:装配失败
      • 使用@Qualifier注解:根据@Qualifier注解中指定的名称作为bean的id进行匹配
        • 能够找到:执行装配
        • 找不到:装配失败
@Controller
public class UserController {
   
   
    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;
    public void saveUser(){
   
   
    userService.saveUser();
    }
}
  • @Autowired中有属性required,默认值为true,因此在自动装配无法找到相应的bean时,会装 配失败
  • 可以将属性required的值设置为true,则表示能装就装,装不上就不装,此时自动装配的属性为 默认值
  • 但是实际开发时,基本上所有需要装配组件的地方都是必须装配的,用不上这个属性。

3、AOP

3.1、场景模拟

3.1.1、声明接口

声明计算器接口Calculator,包含加减乘除的抽象方法

public interface Calculator {
   
   
    int add(int i, int j);
    int sub(int i, int j);
    int mul(int i, int j);
    int div(int i, int j);
}

3.1.2、创建实现类

image-20220929125619708

public class CalculatorPureImpl implements Calculator {
   
   
    @Override
    public int add(int i, int j) {
   
   
    int result = i + j;
    System.out.println("方法内部 result = " + result);
    return result;
}
    @Override
    public int sub(int i, int j) {
   
   
    int result = i - j;
    System.out.println("方法内部 result = " + result);
    return result;
    }
    @Override
    public int mul(int i, int j) {
   
   
    int result = i * j;
    System.out.println("方法内部 result = " + result);
    return result;
    }
    @Override
    public int div(int i, int j) {
   
   
    int result = i / j;
    System.out.println("方法内部 result = " + result);
    return result;
    }
}

3.1.3、创建带日志功能的实现类

image-20220929125736765

public class CalculatorLogImpl implements Calculator {
   
   
    @Override
    public int add(int i, int j) {
   
   
    System.out.println("[日志] add 方法开始了,参数是:" + i + "," + j);
    int result = i + j;
    System.out.println("方法内部 result = " + result);
    System.out.println("[日志] add 方法结束了,结果是:" + result);
    return result;
    }
    @Override
    public int sub(int i, int j) {
   
   
    System.out.println("[日志] sub 方法开始了,参数是:" + i + "," + j);
    int result = i - j;
    System.out.println("方法内部 result = " + result);
    System.out.println("[日志] sub 方法结束了,结果是:" + result);
    return result;
    }
    @Override
    public int mul(int i, int j) {
   
   
    System.out.println("[日志] mul 方法开始了,参数是:" + i + "," + j);
    int result = i * j;
    System.out.println("方法内部 result = " + result);
    System.out.println("[日志] mul 方法结束了,结果是:" + result);
    return result;
    }
    @Override
    public int div(int i, int j) {
   
   
    System.out.println("[日志] div 方法开始了,参数是:" + i + "," + j);
    int result = i / j;
    System.out.println("方法内部 result = " + result);
    System.out.println("[日志] div 方法结束了,结果是:" + result);
    return result;
    }
}
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
11月前
|
弹性计算 Linux 开发者
阿里云海外云服务器购买图文教程指南及优惠购买详解
对于外贸型企业和寻求免备案云服务的用户而言,阿里云海外云服务器无疑是首选方案。那么,如何购买阿里云海外云服务器?在购买过程中能否使用优惠券以节省成本?本文将详细阐述阿里云海外云服务器的购买流程,并展示优惠券的使用效果。
4302 0
|
12月前
|
人工智能 算法 安全
室内外融合定位系统从核心架构、技术原理到部署实施流程等详解(四)
本文详解工业级室内外融合定位系统的精度保障与误差控制,涵盖硬件选型、算法优化与部署策略,分析化工、管廊、制造等场景的应用成效,展现“北斗+UWB”无缝衔接的高精度定位方案及其在安全管理与运营优化中的核心价值。如果您想进一步了解室内外融合定位技术和案例,欢迎搜索维构lbs智能定位~
|
6月前
|
人工智能 缓存 运维
重磅发布丨云监控 AI Agent 可观测,企业生产级 Agent 首选全域观测平台
AI Agent 可观测是面向企业生产级 Agent 的全域观测平台,提供从接入、建模、分析到 Agentic Ops 的全域观测和分析能力,帮助企业彻底打开 Agent 的黑箱,实现 Agent 执行过程的可追踪、可诊断、可优化。
1337 46
|
6月前
|
人工智能 前端开发 数据挖掘
全链路实战:依托Codex完成PPT、数据分析、网页与APP一站式AI开发教程
在AI技术飞速迭代的当下,代码生成早已不是AI工具的单一能力边界。OpenAI旗下的Codex经过持续升级,如今已经成长为一款综合性智能生产力平台,除了经典的代码编写能力外,还支持插件调用、电脑远程操控、数据分析、多媒体制作、全品类应用开发等多元功能。本文将结合完整实操流程,一步步演示如何使用Codex完成PPT制作、体育赛事数据分析预测、网页开发以及移动端APP开发四大核心场景,全程记录操作指令、执行过程、代码实现以及问题优化方案,直观展现AI如何重塑传统工作与开发流程,同时剖析这套全链路AI工作模式的优势与现存局限。整套流程无需深厚的专业功底,普通办公人员、初级开发者都可以参考落地。
1537 1
|
6月前
|
人工智能 JSON 自然语言处理
阿里云百炼产品月报【2026年5月】
本月阿里云百炼平台重磅升级:发布Qwen3.7系列大模型(Max版推理后付费5折)、Qwen3.5实时语音翻译模型及HappyHorse-1.0(8折体验);上线官方CLI工具,支持10+模态一键调用;Token Plan支持多座席共享与精细化管理;MCP广场新增航班、天气等专业服务;金融、法律垂直领域上新20+智能应用模板。
1372 3
|
6月前
|
Oracle 关系型数据库 分布式数据库
云原生数据库PolarDB(兼容Oracle)从零到一:架构解析、实战部署与Oracle语法完全指南
本文系统讲解阿里云云原生数据库PolarDB PostgreSQL版(兼容Oracle)的核心架构与实战使用。首先剖析计算存储分离的Shared Everything架构,解读一写多读集群、自动读写分离和秒级弹性扩缩容的技术原理。随后从零开始演示集群创建、网络安全配置、数据库账号权限管理及三种主流连接方式。在SQL语法部分,详细展开建表语句、NUMBER/VARCHAR2/DATE等Oracle兼容数据类型、基础DML操作、高级查询(多表连接、聚合、子查询、序列)、事务管理与隔离级别。深入讲解PL/SQL编程的完整体系:存储过程与函数创建、异常处理(预定义异常、自定义异常、PRAGMA_EXC
|
6月前
|
人工智能 安全 机器人
【AI 尝鲜实验室】上新 | QwenPaw:本地优先的多渠道个人 AI 助手,让 AI 分身住进你的钉钉与飞书
QwenPaw 是 AgentScope 团队开源的本地优先个人 AI 助手(GitHub 17.4k+ Stars,Apache-2.0 协议),非普通 ChatBot,而是具备长期记忆、多渠道接入(钉钉/飞书/微信/Discord)、本地跑模型、多 Agent 协作能力的“AI 分身”。本实验通过阿里云计算巢一键云端部署,浏览器安全代理即可快速配置使用。
|
6月前
|
存储 弹性计算 文件存储
阿里云云备份(Cloud Backup)全量对接与使用指南
本文系统讲解阿里云云备份(原HBR)从开通、权限配置、客户端部署到控制台与SDK对接的全流程,覆盖ECS、NAS、OSS、本地服务器等主流备份场景,详解备份计划配置、数据恢复、跨账号备份及安全成本优化策略,并附完整代码示例与常见问答,助力企业快速落地混合云数据保护方案。
|
6月前
|
人工智能 运维 安全
云原生 - AI Native 多智能体数字人架构实践
我们以云原生应用部门为试验田,用商业化产品 AgentTeams 落地一支"数字员工小分队",让它们承接日常研发、工单答疑、开源维护与运营等业务,把原本人肉串联的协作流程,做成 AI Native 的工作方式。
834 0
|
6月前
|
人工智能 运维 API
新手必看!阿里云部署OpenClaw实操步骤+百炼Token Plan配置及避坑指南
在AI技术快速普及的当下,OpenClaw(原Clawdbot)作为一款轻量化、高灵活度的大模型智能体框架,凭借低成本、易部署、强适配性等优势,成为个人开发者与中小企业搭建专属AI助手的首选工具。2026年,阿里云针对OpenClaw推出专属预装镜像与一键部署方案,搭配百炼大模型Token Plan套餐,可快速实现7×24小时在线的AI智能体,覆盖文档处理、网页自动化、智能对话、任务编排等多元场景。本文将从零基础视角,详细拆解阿里云部署OpenClaw的完整步骤,同步讲解百炼Token Plan的配置流程与高频避坑要点,全程附带实操代码命令,新手也能30分钟内完成部署上线。
383 0