Java笔记:Spring配置xml和注解(5)

简介: Java笔记:Spring配置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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
>
    <!--开启包扫描-->
    <context:component-scan base-package="com.demo.ioc"/>
</beans>

2、注解方式

package com.demo.ioc;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
// 创建配置文件,可以认为是一个xml配置文件
@Configuration
// 设置需要扫描的包
@ComponentScan(value = "com.demo.ioc")
public class MyConfiguration {
}
package com.demo.ioc;
import org.springframework.stereotype.Component;
// 增加注解后会被Spring扫描到, 默认的Id是类名首字母小写
@Component(value = "person")
public class Person {
}

Bean别名

xml形式

<bean id="person" name="person1, person2" class="com.demo.ioc.Person"/>
<alias name="person" alias="person3"/>

注解方式

package com.demo.ioc;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
// 创建配置文件,可以认为是一个xml配置文件
@Configuration
public class MyConfiguration {
    @Bean(name = {"person1", "person2"})
    public Person person(){
        return new Person();
    }
}

通过注解注入Bean

  • 通过方法注入Bean
  • 通过构造方法注入Bean
  • 通过Set方法注入Bean
  • 通过属性注入Bean
  • 集合类Bean类型注入
  • 直接注入集合实例
  • 将多个泛型的实例注入到集合
  • 将多个泛型实例注入到List
  • 控制泛型示实例在List中的顺序
  • 将多个泛型的实例注入到Map
  • String、Integer等类型直接赋值
  • SpringIoC容器内置接口示例注入

准备Bean

package com.demo.ioc;
import org.springframework.stereotype.Component;
@Component
public class Cat {
}
package com.demo.ioc;
import org.springframework.stereotype.Component;
@Component
public class Dog {
}
package com.demo.ioc;
import org.springframework.stereotype.Component;
@Component
public class Pig {
}

配置文件

package com.demo.ioc;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// 创建配置文件,可以认为是一个xml配置文件
@Configuration
@ComponentScan(value = "com.demo.ioc")
public class MyConfiguration {
    @Bean(name = "stringList")
    public List<String> stringList() {
        List<String> list = new ArrayList<>();
        list.add("Tom");
        list.add("Jack");
        return list;
    }
    // 此优先级高于 List<String>
    @Bean
    @Order(2) // 指定优先级
    public String string1(){
        return "string1";
    }
    @Bean
    @Order(1)
    public String string2(){
        return "string2";
    }
    @Bean
    public Map<String, Integer> stringIntegerMap(){
        Map<String, Integer> map = new HashMap<>();
        map.put("dog", 23);
        map.put("pig", 24);
        return map;
    }
    // 优先级高于 Map<String, Integer>
    @Bean
    public Integer integer1(){
        return 222;
    }
    @Bean
    public Integer integer2(){
        return 223;
    }
}

注入实例

package com.demo.ioc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Component
public class Person {
    private Dog dog;
    // 1、构造方法注入
    @Autowired
    public Person(Dog dog) {
        this.dog = dog;
    }
    private Cat cat;
    // 2、Set方法注入
    @Autowired
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    // 3、通过属性注入
    @Autowired
    private Pig pig;
    private List<String> stringList;
    // 4、注入List类型
    @Autowired
    // 指定Bean的ID
    // @Qualifier(value = "stringList")
    public void setStringList(List<String> stringList) {
        this.stringList = stringList;
    }
    public List<String> getStringList() {
        return stringList;
    }
    // 5、Map类型注入
    private Map<String, Integer> stringIntegerMap;
    public Map<String, Integer> getStringIntegerMap() {
        return stringIntegerMap;
    }
    @Autowired
    public void setStringIntegerMap(Map<String, Integer> stringIntegerMap) {
        this.stringIntegerMap = stringIntegerMap;
    }
    private String name;
    // 6、String、Integer等类型直接赋值
    @Value("Tom") // 如果是Integer会自动转型
    public void setName(String name) {
        this.name = name;
    }
    // 7、容器内置接口注入
    private ApplicationContext context;
    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }
    public ApplicationContext getContext() {
        return context;
    }
    @Override
    public String toString() {
        return "Person{" +
                "dog=" + dog +
                ", cat=" + cat +
                ", pig=" + pig +
                ", stringList=" + stringList +
                ", stringIntegerMap=" + stringIntegerMap +
                ", name='" + name + '\'' +
                '}';
    }
}


测试

package com.demo.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class BeanTest {
    @Test
    public void testBean() {
        // 获取上下文
        ApplicationContext context =
                new AnnotationConfigApplicationContext(MyConfiguration.class);
        // 获取Bean
        Person bean = context.getBean("person", Person.class);
        System.out.println(bean);
        System.out.println(bean.getStringList());
        System.out.println(bean.getStringIntegerMap());
        System.out.println(bean.getContext().getBean("cat"));
    }
}

测试输出

Person{dog=com.demo.ioc.Dog@441772e, 
    cat=com.demo.ioc.Cat@7334aada, 
    pig=com.demo.ioc.Pig@1d9b7cce, 
    stringList=[string2, string1], 
    stringIntegerMap={integer1=222, integer2=223}, 
    name='Tom'
}
[string2, string1]
{integer1=222, integer2=223}
com.demo.ioc.Cat@7334aada
相关文章
|
10月前
|
监控 Java 数据库
从零学 Dropwizard:手把手搭轻量 Java 微服务,告别 Spring 臃肿
Dropwizard 整合 Jetty、Jersey 等成熟组件,开箱即用,无需复杂配置。轻量高效,启动快,资源占用少,内置监控、健康检查与安全防护,搭配 Docker 部署便捷,是构建生产级 Java 微服务的极简利器。
993 117
|
9月前
|
安全 前端开发 Java
《深入理解Spring》:现代Java开发的核心框架
Spring自2003年诞生以来,已成为Java企业级开发的基石,凭借IoC、AOP、声明式编程等核心特性,极大简化了开发复杂度。本系列将深入解析Spring框架核心原理及Spring Boot、Cloud、Security等生态组件,助力开发者构建高效、可扩展的应用体系。(238字)
|
10月前
|
人工智能 Java API
构建基于Java的AI智能体:使用LangChain4j与Spring AI实现RAG应用
当大模型需要处理私有、实时的数据时,检索增强生成(RAG)技术成为了核心解决方案。本文深入探讨如何在Java生态中构建具备RAG能力的AI智能体。我们将介绍新兴的Spring AI项目与成熟的LangChain4j框架,详细演示如何从零开始构建一个能够查询私有知识库的智能问答系统。内容涵盖文档加载与分块、向量数据库集成、语义检索以及与大模型的最终合成,并提供完整的代码实现,为Java开发者开启构建复杂AI智能体的大门。
5429 58
|
9月前
|
消息中间件 缓存 Java
Spring框架优化:提高Java应用的性能与适应性
以上方法均旨在综合考虑Java Spring 应该程序设计原则, 数据库交互, 编码实践和系统架构布局等多角度因素, 旨在达到高效稳定运转目标同时也易于未来扩展.
753 8
|
XML Java 数据格式
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
Spring 第二节内容补充 关于Bean配置的更多内容和细节 万字详解!
863 18
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
|
XML Java 数据格式
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
574 6
|
XML 前端开发 Java
Spring xml配置
Spring xml配置
|
XML Java 数据格式
Spring高手之路18——从XML配置角度理解Spring AOP
本文是全面解析面向切面编程的实践指南。通过深入讲解切面、连接点、通知等关键概念,以及通过XML配置实现Spring AOP的步骤。
484 6
Spring高手之路18——从XML配置角度理解Spring AOP
|
XML Java 数据格式
手动开发-简单的Spring基于XML配置的程序--源码解析
手动开发-简单的Spring基于XML配置的程序--源码解析
384 0
|
XML Java 数据格式
Spring IOC—基于XML配置和管理Bean 万字详解(通俗易懂)
Spring 第二节 IOC—基于XML配置和管理Bean 万字详解!。
1397 5