Spring有哪些配置方式
1、XML 配置文件。
Bean 所需的依赖项和服务在 XML 格式的配置文件中指定。这些配置文件通常包含许多 bean 定义和特定于应用程序的配置选项。它们通常以 bean 标签开头。例如:
<bean id="studentBean" class="org.edureka.firstSpring.StudentBean">
<property name="name" value="Edureka"></property>
</bean>
2、注解配置。
您可以通过在相关的类,方法或字段声明上使用注解,将 Bean 配置为组件类本身,而不是使用 XML 来描述 Bean 装配。默认情况下,Spring 容器中未打开注解装配。因此,您需要在使用它之前在 Spring 配置文件中启用它。例如:
<beans>
<context:annotation-config/>
<!-- bean definitions go here -->
</beans>
@Service
@Component
@Repository
@Controlle
3、Java Config 配置。
Spring 的 Java 配置是通过使用 @Bean 和 @Configuration 来实现。
@Bean注解扮演与<bean />元素相同的角色。用到方法上,表示当前方法的返回值是一个bean@Configuration类允许通过简单地调用同一个类中的其他@Bean方法来定义 Bean 间依赖关系。相当于spring的配置文件XML
例如:
@Configuration
public class StudentConfig { @Bean
public StudentBean myStudent() {
return new StudentBean();
} }
这几种方式没有什么所谓的优劣之分,主要看使用情况,一般来说是这样:
涉及到全局配置的,例如数据库相关配置、MVC相关配置等,就用Java Config 配置的方式
涉及到业务配置的,就使用注解方式
*****************************************************************************
*****************************************************************************
Java Config 配置 解释:https://blog.csdn.net/peng86788/article/details/81188049
定义 JavaConfig 类 对于一个 POJO 类,在类上使用@Configuration 注解,将会使当前类作为一个 Spring 的容器来使用,用于完成 Bean 的创建。在该 JavaConfig 的方法上使用@Bean,将会使一个普通方法所返回的结果变为指定名称的 Bean 实例。
package com.lzl.spring.entity; import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; //该注解表示这个类为javaConfig类
@Configuration
public class MyConfig {
//该注解表示:向容器中注册一个叫做myCar的对象
@Bean("myCar")
public Car getCar() {
return new Car("保时捷","911",300);
}
//该注解表示:向容器中注册一个叫做person的对象
//并且通过byType的方式注入car
@Bean(name="person",autowire=Autowire.BY_TYPE)
public Person getPerson() {
return new Person(1001,"望穿秋水见伊人");
}
}
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.lzl.spring" /> </beans>
测试类
package com.lzl.spring.test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lzl.spring.entity.Car;
import com.lzl.spring.entity.Person; public class SpringTest {
@Test
public void test1() {
//读取配置文件
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-config.xml");
Car car = ctx.getBean("myCar", Car.class);
System.out.println(car);
Person person = ctx.getBean("person", Person.class);
System.out.println(person);
}
}
控制台输出

Spring有哪些配置方式的更多相关文章
- SpringBoot学习(二)-->Spring的Java配置方式
二.Spring的Java配置方式 Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.@Configuration 和 @Bean Spring的Java配置方式是通过 @ ...
- Spring 的java 配置方式
Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.1@Configuration 和 @Bean Spring的Java配置方式是通过 @Configuration 和 @ ...
- Spring的Java配置方式—@Configuration和@Bean实现Java配置
Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.@Configuration 和 @BeanSpring的Java配置方式是通过 @Configuration 和 @Be ...
- SpringBoot学习(二)——Spring的Java配置方式
Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 一.@Configuration 和 @Bean Spring的Java配置方式是通过@Configuration和@Bean ...
- Spring AOP 不同配置方式产生的冲突问题
Spring AOP的原理是 JDK 动态代理和CGLIB字节码增强技术,前者需要被代理类实现相应接口,也只有接口中的方法可以被JDK动态代理技术所处理:后者实际上是生成一个子类,来覆盖被代理类,那么 ...
- Spring的Java配置方式
Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1 @Configuration 和 @Bean Spring的Java配置方式是通过 @Configuration ...
- Spring注解和配置方式
Spring提供了一个org.springframework.beans.factory.FactoryBean工厂类接口,用户可以通过实现该接口定制实例化Bean的逻辑. 从Spring3.0开始, ...
- Spring MVC Controller配置方式
第一种 URL对应Bean如果要使用此类配置方式,需要在XML中做如下样式配置 以上配置,访问/hello.do就会寻找ID为/hello.do的Bean,此类方式仅适用小型的应用系统 第二种 为UR ...
- SpringBoot学习(三)-->Spring的Java配置方式之读取外部的资源配置文件并配置数据库连接池
三.读取外部的资源配置文件并配置数据库连接池 1.读取外部的资源配置文件 通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值,具体用法: @Configuration ...
- Spring -- 三种配置方式
1.Explicit configuration in XML:显示的XML配置. 优点: 1)XML配置方式进一步降低了耦合,使得应用更加容易扩展,即使对配置文件进一步修改也不需要工程进行修改和重新 ...
随机推荐
- NOI4.6 1455:An Easy Problem
描述 As we known, data stored in the computers is in binary form. The problem we discuss now is about ...
- VScode前端插件推荐
工欲善其事,必先利其器,安利一波前端插件. Chinese (Simplified) Language Pack for Visual Studio CodeVScode汉化插件 Beautify代码 ...
- 架构模式中的Active Record和Data Mapper
架构模式中的Active Record和Data Mapper 概念 在简单应用中,领域模型是一种和数据库结构一致的简单结构,对应每个数据库表都有一个领域类,在这种情况下,有必要让每个对象负责数据库的 ...
- C++从array数组向vector向量复制元素的两种方式
#include <iostream> #include <vector> using namespace std; int main() { const int arr_si ...
- HDU-5902-GCD is Funny解题笔记
Alex has invented a new game for fun. There are n integers at a board and he performs the following ...
- <状压DP>solution-POJ3311_Hie with the Pie
Hie with the Pie Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers ...
- C#系列之占位符的使用方法(二)
今天,我将简单记录下占位符的使用方法 首先,我们来看不使用占位符的方法来代码输出 int number = 10; int number_1 = 20; int number_2 = 30; Cons ...
- CAD制图系列之“点”的绘制方法
本章将重点记录点的绘制方法 画直线的原理其实在画两个点,所以,只要会把握点的画法,线就画好啦 点输入模式: 鼠标输入方式: 手动模式: 自动模式: 键盘输入方式: 绝对直角坐标 相对直角坐标 绝对极坐 ...
- jmeter处理http请求Content-Type类型和传参方式
引言 我们在做接口测试的时候经常会忽略数据类型content-type的格式,以及参数Parameters和Body Data的区别和用途. 对于初次接触接口的同学来说,自己在发送一个http请求时, ...
- GetModuleFileNameEx遍历获取64bit程序路径失败的一种解决方法(Win7-64-bit)
问题: 32位程序在64位系统上调用GetModuleFileNameEx()遍历获取64位进程的全路径失败,得到的路径都为空. 根据官方的说法: For the best results use t ...