Spring框架-Spring Bean管理

2024-01-04

Spring Bean管理

Spring Bean

通过之前的学习对SpringBean有了简单的认识。下面详细介绍一下SpringBean。

Spring Bean是任何由Spring容器管理的对象。可以通过配置文件,注解或者Java代码进行配置和创建。

在Spring中,Bean是Spring容器中的基本单位,Spring容器负责管理所有的Bean,包括创建,初始化,配置,销毁和依赖注入等。所有我们掌握bean的概念和使用方法对于理解和应用Spring框架是非常重要的。

Bean的定义

在Spring中,Bean的定义通常包括以下几个元素:

  • ID:Bean的唯一标识符。
  • Class:Bean的类型,通常是一个Java类。
  • Scope:Bean的作用域,通常有单例(Singleton)、原型(Prototype)、会话(Session)和请求(Request)四种。默认为单例。
  • Constructor arguments:Bean的构造函数参数。
  • Properties:Bean的属性。

Bean的依赖注入

在Spring中,依赖注入是一种将对象之间的依赖关系交给Spring容器管理的方式。当一个Bean需要使用另一个Bean时,它只需声明一个对该Bean的引用,并由Spring容器负责注入该Bean的实例。

Spring支持多种注入方式,包括构造函数注入、Setter方法注入、字段注入和接口注入等。其中,构造函数注入和Setter方法注入是最常用的两种方式。

配置方式:

Spring提供了多种方式来配置和创建Bean,包括XML配置、注解配置和Java配置。具体方法如下:

  • XML配置:通过在XML配置文件中定义Bean的标签和属性,可以告诉Spring如何创建和管理Bean。
  • 注解配置:通过在Bean的类上添加注解来告诉Spring如何创建和管理Bean。
  • Java配置:通过编写Java类来告诉Spring如何创建和管理Bean。
使用XML配置方式:
User.java
package com.sin.pojo;

/**
 * @createTime 2024/1/2 15:56
 * @createAuthor SIN
 * @use
 */
public class User {

    private String name;
    private int age;

    public User(String name, int age){
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><!-- XML声明,xml版本和编码格式 -->

<!-- spring配置文件起点  -->
<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,对应的类是com.sin.pojo.User   -->
    <bean id="user" class="com.sin.pojo.User">
        <!-- 
            通过构造方法注入两个参数值,
            0索引是String name,参数值为张三
            1索引是int age ,参数只为20
                  -->
        <constructor-arg index="0" value="张三"/>
        <constructor-arg index="1" value="20"/>
    </bean>


</beans>
UserTest.java
package com.sin.test;

import com.sin.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @createTime 2024/1/2 16:00
 * @createAuthor SIN
 * @use
 */
public class UserTest {

    @Test
    public void test() {
        // 加载Spring配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 获取bean
        User user = (User) context.getBean("user");

        // 使用bean输出数据
        System.out.println("姓名:" + user.getName());
        System.out.println("年龄:" + user.getAge());
    }
}

使用注解配置方式:

通过在Bean的类上添加注解来告诉Spring如何创建和管理Bean,常见的注解有:

注解 说明
@Component 是Spring框架的基本注解之一,用于将一个类标识为组件(Component)。被 @Component 注解标记的类将会被Spring容器自动扫描,并创建对应的Bean。它通常用于标记普通的Java类,表示这些类需要由Spring进行管理。
@Service @Component 注解的一个特化版本,用于标记一个类为服务(Service)层的组件。它表示该类是业务逻辑的实现类,通常用于在应用程序的服务层中使用。被 @Service 注解标记的类将被自动注册为Bean,并可以通过依赖注入在其他类中使用。
@Repository @Component 注解的一个特化版本,用于标记一个类为持久层(Repository)的组件。它表示该类用于访问和操作数据库或其他数据存储。被 @Repository 注解标记的类将被自动注册为Bean,并可以通过依赖注入在其他类中使用。
@Autowired 是Spring框架的依赖注入注解,用于自动装配Bean之间的依赖关系。当一个类需要使用其他的Bean时,可以使用 @Autowired 注解在成员变量、构造方法或Setter方法上,Spring容器会自动将合适的Bean注入进来。通过 @Autowired ,我们可以避免手动实例化对象或配置Bean之间的关联关系。
@Configuration 将一个类声明为配置类,用于定义应用程序的配置信息。通常与 @Bean 一起使用。
@Bean 在配置类中使用该注解定义一个Bean。可以通过方法返回一个对象,并指定Bean的名称和类型。
@Scope 用于指定Bean的作用域,即Bean的生命周期范围,如单例、原型等。常见的作用域包括 Singleton (默认)、 Prototype Request Session 等。
@Value 用于注入配置值或表达式到Bean属性中。可以用于从配置文件中获取值,或者使用SpEL表达式进行计算。
@Qualifier 用于指定要注入的Bean的名称,当存在多个同类型的Bean时,可以通过该注解指定要注入的具体Bean。
@Primary 用于标识某个Bean为首选的Bean,当存在多个同类型的Bean时,优先选择被 @Primary 注解标识的Bean。
@Component

UserService.java

package com.sin.service;

import org.springframework.stereotype.Component;

/**
 * @createTime 2024/1/2 16:42
 * @createAuthor SIN
 * @use
 */
@Component
public class UserService {

    public String getUserInfo(){
        return "User information";
    }
}

UserTest.java

package com.sin.test;

import com.sin.pojo.User;
import com.sin.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @createTime 2024/1/2 16:00
 * @createAuthor SIN
 * @use
 */
public class UserTest {

    @Test
    public void test() {
        // 加载Spring配置文件
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        // 注册UserService类
        context.register(UserService.class);

        // 启动Spring容器
        context.refresh();

        // 从容器中获取UserService实例
        UserService userService = context.getBean(UserService.class);

        System.out.println(userService.getUserInfo());

        // 关闭Spring容器
        context.close();
    }
}
@Service

UserService.java

package com.sin.service;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @createTime 2024/1/2 16:42
 * @createAuthor SIN
 * @use
 */
@Service
public class UserService {

    public String getUserInfo(){
        return "User information";
    }
}

UserTest.java

package com.sin.test;

import com.sin.pojo.User;
import com.sin.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @createTime 2024/1/2 16:00
 * @createAuthor SIN
 * @use
 */
public class UserTest {

    @Test
    public void test() {
        // 加载Spring配置文件
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        // 注册包含@Service注解的类所在的包路径
        context.scan("com.sin.service");

        // 启动Spring容器
        context.refresh();

        // 从容器中获取UserService实例
        UserService userService = context.getBean(UserService.class);

        System.out.println(userService.getUserInfo());

        // 关闭Spring容器
        context.close();
    }
}
@Repository

UserDao.java

package com.sin.dao;

import org.springframework.stereotype.Repository;

/**
 * @createTime 2024/1/2 16:58
 * @createAuthor SIN
 * @use
 */
@Repository
public class UserDao {
    public String getUserInfo(){
        return "从数据库中查找数据";
    }
}

UserService.java

package com.sin.service;

import com.sin.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @createTime 2024/1/2 16:42
 * @createAuthor SIN
 * @use
 */
@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    public String getUserInfo(){
        return userDao.getUserInfo();
    }
}

UserTest.java

package com.sin.test;

import com.sin.pojo.User;
import com.sin.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @createTime 2024/1/2 16:00
 * @createAuthor SIN
 * @use
 */
public class UserTest {

    @Test
    public void test() {
        // 加载Spring配置文件
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        // 注册包含@Repository注解的类所在的包路径
        context.scan("com.sin.dao");
        // 注册包含@Service注解的类所在的包路径
        context.scan("com.sin.service");

        // 启动Spring容器
        context.refresh();

        // 从容器中获取UserService实例
        UserService userService = context.getBean(UserService.class);

        System.out.println(userService.getUserInfo());

        // 关闭Spring容器
        context.close();
    }
}
@Configuration

UserDao.java

package com.sin.dao;

import org.springframework.stereotype.Repository;

/**
 * @createTime 2024/1/2 16:58
 * @createAuthor SIN
 * @use
 */
@Repository
public class UserDao {
    public String getUserInfo(){
        return "从数据库中查找数据";
    }
}

UserService.java

package com.sin.service;

import com.sin.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @createTime 2024/1/2 16:42
 * @createAuthor SIN
 * @use
 */
@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    public String getUserInfo(){
        return userDao.getUserInfo();
    }
}

AppConfig.java

package com.sin.config;

import com.sin.dao.UserDao;
import com.sin.service.GreetingService;
import com.sin.service.UserService;
import com.sin.service.impl.GreetingServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @createTime 2024/1/2 10:56
 * @createAuthor SIN
 * @use
 */
@Configuration
@ComponentScan// 启动组件扫描,自动注册待用@Respository和@Service注解的类到Spring容器中
public class AppConfig {

    @Bean("userDao")
    public UserDao userDao(){
        return new UserDao();
    }
    @Bean("userService")
    public UserService userService(){
        return new UserService();
    }



}

AppTest.java

package com.sin.test;

import com.sin.config.AppConfig;
import com.sin.service.UserService;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @createTime 2024/1/2 17:14
 * @createAuthor SIN
 * @use
 */
public class AppTest {

    @Test
    public void test(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

        UserService userService = (UserService) context.getBean("userService");


        System.out.println(userService.getUserInfo());

        context.close();
    }
}
@Scope

MyBean.java

package com.sin.pojo;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @createTime 2024/1/3 8:35
 * @createAuthor SIN
 * @use
 */
@Component
@Scope("prototype")
public class MyBean {

    private String name;

    public MyBean(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

@Scope 是 Spring 框架中用于设置 bean 的作用域的注解之一。通过使用 @Scope 注解,我们可以指定 bean 的实例是单例(Singleton)还是原型(Prototype)等不同的作用域。

以下是 @Scope 注解的一些常见用法:

  • @Scope("singleton") :默认选项,表示 bean 是单例的,Spring 容器将只创建并管理一个 bean 实例。
  • @Scope("prototype") :表示 bean 是原型的,每次从容器中获取该 bean 时都会创建一个新的实例。
  • @Scope("request") :表示 bean 的生命周期与 HTTP 请求的生命周期相同,在每个请求处理过程中创建一个新的 bean 实例,并在请求结束后销毁。
  • @Scope("session") :表示 bean 的生命周期与 HTTP 会话的生命周期相同,在每个会话期间创建一个新的 bean 实例,并在会话结束后销毁。
  • @Scope("globalSession") :表示 bean 的生命周期与全局 HTTP 会话的生命周期相同,仅在使用基于 Portlet 的 web 应用程序时才有效。

AppConfig.java

package com.sin.config;

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

/**
 * @createTime 2024/1/2 10:56
 * @createAuthor SIN
 * @use
 */
@Configuration
@ComponentScan(basePackages = "com.sin.pojo") // 扫描组件
public class AppConfig {

}

AppTest.java

package com.sin.test;

import com.sin.config.AppConfig;
import com.sin.pojo.MyBean;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @createTime 2024/1/3 8:41
 * @createAuthor SIN
 * @use
 */
public class AppTest {

    @Test
    public void test(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

         MyBean bean1 = context.getBean(MyBean.class);
         bean1.setName("张三");
        System.out.println(bean1.getName());

        MyBean bean2 = context.getBean(MyBean.class);
        bean2.setName("李四");
        System.out.println(bean2.getName());

    }
}
@Value

app.properties

app.name : lisi

MyBean.java

package com.sin.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @createTime 2024/1/3 8:35
 * @createAuthor SIN
 * @use
 */
@Component
public class MyBean {

    @Value("李四")
    private String name;

    @Value("${app.name}")// 调用app.properties中的app.name
    private String username;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUsername() {
        return username;
    }

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

AppConfig.java

package com.sin.config;

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

/**
 * @createTime 2024/1/2 10:56
 * @createAuthor SIN
 * @use
 */
@Configuration
@ComponentScan(basePackages = "com.sin.pojo") // 扫描组件
@PropertySource("classpath:app.properties")// 指定属性文件的位置或者其他属性值的类型和名称,以便让spring加载其中定义的属性值
public class AppConfig {



}

AppTest.java

package com.sin.test;

import com.sin.config.AppConfig;
import com.sin.pojo.MyBean;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @createTime 2024/1/3 8:41
 * @createAuthor SIN
 * @use
 */
public class AppTest {

    @Test
    public void test(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

        MyBean bean1 = context.getBean(MyBean.class);
        System.out.println(bean1.getName());
        System.out.println(bean1.getUsername());



    }
}

@Qualifier

UserService.java

package com.sin.service;

/**
 * @createTime 2024/1/3 9:10
 * @createAuthor SIN
 * @use
 */
public interface UserService {
    public void addUser(String username);
}

UserServiceImpl1.java

package com.sin.service.impl;

import com.sin.service.UserService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * @createTime 2024/1/3 9:10
 * @createAuthor SIN
 * @use
 */
@Service
@Qualifier("userServiceImpl1")
public class UserServiceImpl1 implements UserService {


    @Override
    public void addUser(String username) {
        System.out.println("添加的数据为"+ username + "位于 userServiceImpl1");
    }
}

UserServiceImpl2.java

package com.sin.service.impl;

import com.sin.service.UserService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * @createTime 2024/1/3 9:12
 * @createAuthor SIN
 * @use
 */
@Service
@Qualifier("userServiceImpl2")
public class UserServiceImpl2 implements UserService {


    @Override
    public void addUser(String username) {
        System.out.println("添加的数据为"+ username + "位于 userServiceImpl2");
    }
}

AppConfig.java

package com.sin.config;

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

/**
 * @createTime 2024/1/2 10:56
 * @createAuthor SIN
 * @use
 */
@Configuration
@ComponentScan(basePackages = "com.sin.pojo") // 扫描组件
public class AppConfig {



}

AppTest.java

package com.sin.test;

import com.sin.config.AppConfig;
import com.sin.pojo.UserServiceClient;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @createTime 2024/1/3 8:41
 * @createAuthor SIN
 * @use
 */
public class AppTest {

    @Test
    public void test(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

        UserServiceClient userServiceClient = context.getBean(UserServiceClient.class);

        userServiceClient.addUser("张三");
    }
}
@Primary

UserService.java

package com.sin.service;

/**
 * @createTime 2024/1/3 9:10
 * @createAuthor SIN
 * @use
 */
public interface UserService {
    public void addUser(String username);
}

UserServiceImpl1.java

package com.sin.service.impl;

import com.sin.service.UserService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

/**
 * @createTime 2024/1/3 9:10
 * @createAuthor SIN
 * @use
 */
@Service
public class UserServiceImpl1 implements UserService {


    @Override
    public void addUser(String username) {
        System.out.println("添加的数据为"+ username + "位于 userServiceImpl1");
    }
}

UserServiceImpl2.java

package com.sin.service.impl;

import com.sin.service.UserService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

/**
 * @createTime 2024/1/3 9:12
 * @createAuthor SIN
 * @use
 */
@Service
@Primary
public class UserServiceImpl2 implements UserService {


    @Override
    public void addUser(String username) {
        System.out.println("添加的数据为"+ username + "位于 userServiceImpl2");
    }
}

UserServiceClient.java

package com.sin.pojo;

import com.sin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @createTime 2024/1/3 9:15
 * @createAuthor SIN
 * @use
 */
@Component
public class UserServiceClient {

    private final UserService userService;


    public UserServiceClient(UserService userService) {
        this.userService = userService;
    }

    public void addUser(String username){
        userService.addUser(username);
    }
}

AppConfig.java

package com.sin.config;

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

/**
 * @createTime 2024/1/2 10:56
 * @createAuthor SIN
 * @use
 */
@Configuration
@ComponentScan(basePackages = "com.sin") // 扫描组件
public class AppConfig {



}

AppTest.java

package com.sin.test;

import com.sin.config.AppConfig;
import com.sin.pojo.UserServiceClient;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @createTime 2024/1/3 8:41
 * @createAuthor SIN
 * @use
 */
public class AppTest {

    @Test
    public void test(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

        UserServiceClient userServiceClient = context.getBean(UserServiceClient.class);

        userServiceClient.addUser("张三");
    }
}

Bean的作用域和生命周期:

Bean的作用域

Spring中Bean的作用域通常有四种:

  • 单例(Singleton):在整个应用程序中只创建一个Bean实例。
  • 原型(Prototype):每次请求Bean时都会创建一个新的实例。
  • 会话(Session):在Web应用程序中,每个会话创建一个Bean实例。
  • 请求(Request):在Web应用程序中,每个请求创建一个Bean实例。

Spring Bean的生命周期包括以下几个阶段:

  • 实例化:Spring容器根据Bean的定义,使用构造函数或者工厂方法创建Bean的实例。
  • 属性赋值:Spring容器将Bean的属性值注入到Bean实例中。
  • 初始化:Spring容器调用Bean的初始化方法,可以通过实现InitializingBean接口或者在配置文件中指定init-method来定义初始化方法。
  • 使用:Bean处于可用状态,可以被其他对象引用。
  • 销毁:Spring容器调用Bean的销毁方法,可以通过实现DisposableBean接口或者在配置文件中指定destroy-method来定义销毁方法。

BeanFactory

BeanFactory是Spring框架中的核心接口之一,Bean工厂用来管理和获取Spring Bean。它提供了一种延迟加载,懒加载的机制,只有在需要Bean的时候才会实例化和初始化。

BeanFacroty接口中常用的方法有:

方法 说明
getBean(String name) 根据给定的 Bean 名称获取对应的 Bean 实例
getBean(String name, Class requiredType) 根据给定的 Bean 名称和类型获取对应的 Bean 实例
boolean containsBean(String name) 判断容器中是否包含指定名称的 Bean
boolean isSingleton(String name) 判断指定名称的 Bean 是否为单例模式
boolean isPrototype(String name) 判断指定名称的 Bean 是否为原型模式
Class<?> getType(String name) 获取指定名称的 Bean 的类型
String[] getAliases(String name) 获取指定名称的 Bean 的别名

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><!-- XML声明,xml版本和编码格式 -->

<!-- spring配置文件起点  -->
<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">


    <bean id="user" class="com.sin.pojo.User">
    </bean>


</beans>

AppTest.java

package com.sin.test;

import com.sin.config.AppConfig;
import com.sin.pojo.User;
import com.sin.pojo.UserServiceClient;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @createTime 2024/1/3 8:41
 * @createAuthor SIN
 * @use
 */
public class AppTest {

    @Test
    public void test() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");

        System.out.println("获取的bean地址:" + beanFactory.getBean("user"));
        System.out.println("获取的bean地址:" + beanFactory.getBean("user", User.class));
        System.out.println("容器中是否存在user Bean"+beanFactory.containsBean("user"));
        System.out.println("是否为单例模式"+beanFactory.isSingleton("user"));
        System.out.println("是否为原型模式"+beanFactory.isPrototype("user"));
        System.out.println("获取到的类型为:"+beanFactory.getType("user"));
        System.out.println("获取到的别名为:"+beanFactory.getAliases("user"));

    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring框架-Spring Bean管理 的相关文章

  • 使用 GWT 读取非常大的本地 XML 文件

    我正在使用 GWT 构建我的第一个 Java 应用程序 它必须从一个非常大的 XML 文件中读取数据 当我尝试发送对文件中信息的请求时遇到问题 并且我不太确定它是否与文件的大小或我的语义有关 在我的程序中 我有以下内容 static fin
  • “java.net.MalformedURLException:未找到协议”读取到 html 文件

    我收到一个错误 java net MalformedURLException Protocol not found 我想读取网络上的 HTML 文件 mainfest uses permission android name android
  • 如何在 Antlr4 中为零参数函数编写语法

    我的函数具有参数语法 如下面的词法分析器和解析器 MyFunctionsLexer g4 lexer grammar MyFunctionsLexer FUNCTION FUNCTION NAME A Za z0 9 DOT COMMA L
  • Spring Data JPA 选择不同

    我有一个情况 我需要建立一个select distinct a address from Person a 其中地址是 Person 内的地址实体 类型的查询 我正在使用规范动态构建我的 where 子句并使用findAll Specifi
  • Spring Boot自动装配存储库始终为空[重复]

    这个问题在这里已经有答案了 每次我进入我的服务类时 存储库似乎都没有自动连接 因为它不断抛出 NullPointerException 谁能帮我检查一下我缺少什么吗 这是我的代码 演示应用程序 java package com exampl
  • 身份验证在 Spring Boot 1.5.2 和 Oauth2 中不起作用

    我正在使用带有 spring boot 1 5 2 RELEASE 的 Oauth2 当我尝试重写 ResourceServerConfigurerAdapter 类的配置方法时 它给了我一个编译错误 但这在 Spring boot 1 2
  • 在 Wildfly 中与 war 部署共享 util jar 文件

    假设我有一个名为 util jar 的 jar 文件 该 jar 文件主要包含 JPA 实体和一些 util 类 无 EJB 如何使这个 jar 可用于 Wildfly 中部署的所有 war 无需将 jar 放置在 war 的 WEB IN
  • 来自十六进制代码的 Apache POI XSSFColor

    我想将单元格的前景色设置为十六进制代码中的给定颜色 例如 当我尝试将其设置为红色时 style setFillForegroundColor new XSSFColor Color decode FF0000 getIndexed 无论我在
  • 是否可以通过编程方式查找 logback 日志文件?

    自动附加日志文件以支持电子邮件会很有用 我可以以编程方式设置路径 如以编程方式设置 Logback Appender 路径 https stackoverflow com questions 3803184 setting logback
  • 内部存储的安全性如何?

    我需要的 对于 Android 我需要永久保存数据 但也能够编辑 并且显然是读取 它 用户不应访问此数据 它可以包含诸如高分之类的内容 用户不得对其进行编辑 我的问题 我会 并且已经 使用过Internal Storage 但我不确定它实际
  • GWT 2.3 开发模式 - 托管模式 JSP 编译似乎不使用 java 1.5 兼容性

    无法编译 JSP 类 生成的 servlet 错误 DefaultMessage 上次更新 0 日期 中 0 时间 HH mm ss z 语法 错误 注释仅在源级别为 1 5 时可用 在尝试以开发模式在 Web 浏览器中打开我的 gwt 模
  • QuerySyntaxException:无法找到类

    我正在使用 hql 生成 JunctionManagementListDto 类的实际 Java 对象 但我最终在控制台上出现以下异常 org hibernate hql internal ast QuerySyntaxException
  • 如何在 Java 中创建接受多个值的单个注释

    我有一个名为 Retention RetentionPolicy SOURCE Target ElementType METHOD public interface JIRA The Key Bug number JIRA referenc
  • Spring-ws:如何从没有“Request”元素的 xsd 创建 Wsdl

    尝试为客户端实现 SOAP Web 服务 我需要一个 wsdl 文件来通过soapUI 测试该服务 但正如您在下面看到的 这个 xsd 没有 Request 和 Response 方法 所有请求和响应都被定义为基本 ServiceProvi
  • Hamcrest Matchers - 断言列表类型

    问题 我目前正在尝试使用 Hamcrest Matchers 来断言返回的列表类型是特定类型 例如 假设我的服务调用返回以下列表 List
  • OpenCSV:将嵌套 Bean 映射到 CSV 文件

    我正在尝试将 bean 映射到 CSV 文件 但问题是我的 bean 具有其他嵌套 bean 作为属性 所发生的情况是 OpenCSV 遍历属性找到一个 bean 然后进入其中并映射该 bean 内的所有数据 如果找到另一个 bean 它就
  • 如何重新启动死线程? [复制]

    这个问题在这里已经有答案了 有哪些不同的可能性可以带来死线程回到可运行状态 如果您查看线程生命周期图像 就会发现一旦线程终止 您就无法返回到新位置 So 没有办法将死线程恢复到可运行状态 相反 您应该创建一个新的 Thread 实例
  • Java中HashMap和ArrayList的区别?

    在爪哇 ArrayList and HashMap被用作集合 但我不明白我们应该在哪些情况下使用ArrayList以及使用时间HashMap 他们两者之间的主要区别是什么 您具体询问的是 ArrayList 和 HashMap 但我认为要完
  • 配置“DataSource”以使用 SSL/TLS 加密连接到 Digital Ocean 上的托管 Postgres 服务器

    我正在尝试托管数据库服务 https www digitalocean com products managed databases on 数字海洋网 https en wikipedia org wiki DigitalOcean 创建了
  • 在浏览器刷新中刷新检票面板

    我正在开发一个付费角色系统 一旦用户刷新浏览器 我就需要刷新该页面中可用的统计信息 统计信息应该从数据库中获取并显示 但现在它不能正常工作 因为在页面刷新中 java代码不会被调用 而是使用以前的数据加载缓存的页面 我尝试添加以下代码来修复

随机推荐

  • <sa8650>sa8650 CDT-之-汽车CDT配置用户指南(上)

    sa8650 sa8650 CDT 之 汽车CDT配置用户指南 上 2 CDT概述 2 1 Platform ID值 2 2 CDT一般结构 2 3 CDT头 2 4 块元数据 2 5 CDBs 2 6 加载CDT的启动过程
  • 实现智能化运维的关键驱动力,你知道可观测性工具吗

    可观测性是指根据系统产生的数据评估内部系统状态的能力 对于IT运维团队来说 可观测性工具是非常重要的 通过这些工具 IT团队可以同时观察或深入了解IT基础架构中不同应用程序和资源的健康状况和状态 从而主动检测异常 分析问题并解决问题 可观测
  • linux centos使用rz、sz命令上传下载文件

    一般情况下 我们会使用终端软件 如 XShell SecureCRT 或 FinalShell 来连接远程服务器后 使用 rz 命令上传本地文件到远程服务器 再解压发版上线 一 安转使用 系统 Linux CentOS 7 安装 rz 和
  • Metasploit使用msfconsole命令启动时,报错‘/usr/share/metasploit-framework/......’

    当使用msfconsole命令启动时 msf无法启动 且报错 解决办法 1 更新apt程序库 apt get update 2 更新metasploit 框架 apt get install metasploit framework 3 重
  • 【C++项目】【报错】[错误] new: No such file or directory, compilation terminated【及解决方法】

    一 问题描述 C源代码文件在编译过程中报错 错误 new No such file or directory compilation terminated 代码如下 include
  • 第8章 多媒体嵌入

    学习目标 了解视频 音频嵌入技术 能够总结HTML5视频 音频嵌入技术的优点 了解常用的视频文件格式和音频文件格式 能够归纳HTML5支持的视频和音频格式 掌握HTML5中视频的嵌入方法 能够在HTML5页面中添加视频文件 掌握HTML5中
  • 服务器的丢包率高怎么办

    网络出现丢包状况了怎么办 具体情况可以从以下几点来判断 1 有可能是线路故障导致的 所以可以用光纤打光仪先判断是否是光纤的问题 2 对于设备方面来说 很多都是网络接口的光纤接触不良等 3 也可能是操作系统的问题 比如网卡问题和网络运营线路问
  • element ui弹窗在别的弹窗下方,优先级不高的问题

    在 弹窗 的标签中加入append to body即可解决该问题
  • Python基础(十六、数据容器汇总)

    文章目录 一 数据容器汇总 二 数据容器通用操作 1 遍历 2 通用统计 len 容器 max 容器 min 容器
  • 解决重定向导致的cookie丢失

    转发前加上 String s response getHeader Set Cookie s HttpOnly Secure SameSite None response setHeader Set Cookie s
  • Java编写CS架构学生管理系统

    一 环境准备 工具 eclipse navicat 环境 jdk8 数据库 mysql5 7 二 正式开始 第一步 分析需求 就是我们需要知道该干什么 登录功能 对学生信息增删改查操作 第二步 创建项目StudentManager 由于我们
  • catkin_make 编译报错 Unable to find either executable ‘empy‘ or Python module ‘em‘...

    文章目录 写在前面 一 问题描述 二 解决方法 参考链接 写在前面 自己的测试环境 Ubuntu20 04 一 问题描述 自己安装完 anaconda 后 再次执行 catkin make 遇到如下问题 CMake Error at opt
  • How to collect data

    How to collect data 爬虫 Java Python 反爬虫 自动化测试工具 Selenium QMetry Automation Studio Te
  • 【ESP32接入国产大模型之文心一言】

    1 怎样接入文心一言 随着人工智能技术的不断发展 自然语言处理领域也得到了广泛的关注和应用 在这个领域中 文心一言作为一款强大的自然语言处理工具 具有许多重要的应用价值 本文将重点介绍如何通过ESP32接入国产大模型之文心一言api 以及其
  • 公司PCB设计需要外包,需要准备哪些资料给PCB设计公司呢?

    现阶段许多公司仍然是让硬件工程师来进行PCB设计和方案开发 除开这些 硬件工程师还要做更多的专业工作 这样势必会使产品上市的时间大大延长 而且现在随着高速数字电子技术的发展 对高速PCB设计的要求也越高 信号完整性仿真分析 nbsp 时序分
  • 14.10-其他阻塞和非阻塞混合使用的原则

    其他阻塞和非阻塞混合使用的原则 1 同时使用阻塞和非阻塞赋值 2 对同一变量既阻塞赋值又非阻塞赋值 综合出错 原则5 不要在同一个always块中同时使用阻塞和非阻塞赋值 1 同时使用阻塞和非阻塞赋值 Verilog语法并没有禁止将阻塞和非
  • DriveMLM

    本人转载于大佬 大型语言模型为智能驾驶开辟了新的格局 赋予了他们类似人类的思维和认知能力 本文深入研究了大型语言模型 LLM 在自动驾驶 AD 中的潜力 进而提出了DriveMLM 这是一种基于LLM的AD框架 可以在仿真环境中实现闭环自动
  • 协议茶馆:TLV 格式及编码

    本篇是多年前的存篇 出处不详 旧酒换新瓶 温故知新 有了新的理解 一 什么是 TLV 格式 几乎所有的通信都有协议 而几乎所有的需要在卡片和终端之间传送的数据 结构 都是 TLV 格式的 TLV 是 tag length 和 value 的
  • Spring框架-入门(IOC,DI)

    文章目录 Spring框架 简介 创建Spring项目 理解IOC和DI IOC控制反转 示例
  • Spring框架-Spring Bean管理

    文章目录 Spring Bean管理 Spring Bean 配置方式 使用XML配置方式 User java