Spring Boot 框架的第一章Xml 和 JavaConfig

1 什么是 JavaConfig

JavaConfig: 是 Spring 提供的使用 java 类配置容器。 配置 Spring IOC 容器的纯 Java 方法。

优点:

1.可以使用面像对象的方式, 一个配置类可以继承配置类,可以重写方法
2.避免繁琐的 xml 配置

2 Xml 配置容器

创建001-pre-boot 项目

pom.xml

<dependencies>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>5.3.1</version>
	</dependency>
   <dependency>
   	  <groupId>junit</groupId>
   	  <artifactId>junit</artifactId>
   	  <version>4.12</version>
   </dependency>
</dependencies>
<build>
	<plugins>
	  <!-- 编译插件 -->
	  		<plugin>
	  			<artifactId>maven-compiler-plugin</artifactId>
	  			<!-- 插件的版本 -->
	  			<version>3.5.1</version>
	  			<!-- 编译级别 -->
	  			<configuration>
	  				<source>1.8</source>
	  				<target>1.8</target>
	  				<!-- 编码格式 -->
	  				<encoding>UTF-8</encoding>
	  			</configuration>
		  </plugin>		
	 </plugins>
 </build>


创建数据类 Student

public class Student{
	private Integer id;
	private String name;
	private Integer age;
	// set | get




}

resources 目录下创建 Spring 的配置文件 applicationContext.xml

<bean id="myStudent" class="com.bjpowernode.vo.Student">
 	<property name="id" value="1001" />
 	<property name="name" value="李强国" />
 	<property name="age" value="20" />
</bean>

单元测试:

@Test
public void test01(){
	String config="applicationContext.xml";
	ApplicationContext ctx=new 
ClassPathXmlApplicationContext(config);
	Student student = (Student) ctx.getBean("myStudent");
	System.out.println("student="+student);	
	


}	

3 JavaConfig 配置容器

	JavaConfig 主要使用的注解
	@Configuration:放在类的上面, 这个类相当于 xml 配置文件,可以在其中声明 bean
	@Bean:放在方法的上面, 方法的返回值是对象类型, 这个对象注入到 spring ioc 容器 

创建配置类(等同于 xml 配置文件)

/**
* @Configuration:表示当前类是 xml 配置文件的作用
* 在这个类中有很多方法, 方法的返回值是对象。
* 在这个方法的上面加入@Bean, 表示方法返回值的对象放入到容器中。
* @Bean == <bean></bean>
*/





@Configuration
public class SpringConfig {
	// 定义方法, 方法的返回值是对象。  @Bean:表示把对象注入到容器中。 位置: 方法的上面  @Bean 没有使用属性,默认对象名称是方法名




	@Bean
	public Student createStudent(){
		Student student = new Student();
		student.setId(1002);
		student.setName("周仓");
		student.setAge(29);
		return student;
}

// name :指定对象的名称
@Bean(name = "myStudent2")
 public Student makeStudent(){

		Student student = new Student();
		student.setId(1003);
		student.setName("诸葛亮");
		student.setAge(30);
		return student;


}

@Bean
public Date myDate(){
	return new Date();

}

}

}


测试方法:

//使用 JavaConfig
@Test
public void test02(){
	//没有 xml 配置文件,使用 java 类代替 xml 配置文件 的作用
	ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
	Student student = (Student) ctx.getBean("createStudent");
	System.out.println("student==="+student);
	}
@Test
public void test03(){
  //没有 xml 配置文件,使用 java 类代替 xml 配置文件 的作用


		ApplicationContext ctx = new 	AnnotationConfigApplicationContext(SpringConfig.class);
		Student student = (Student) ctx.getBean("myStudent2");
		System.out.println("myStudent2==="+student);
		Object myDate = ctx.getBean("myDate");
		System.out.println("myDate = " + myDate);

}












}

4 @ImportResource

@ImportResource 是导入 xml 配置,等同于 xml 文件的 resources
创建数据类:

public class Cat {
		 private String cardId;
		 private String name;
		 private Integer age;
		  // set |get

}

创建配置文件 beans.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="myCat" class="com.bjpowernode.vo.Cat">
		<property name="cardId" value="XSET29001" />
		<property name="name" value="tom 猫"/>
		<property name="age" value="3" />
	</bean>

</beans>

创建配置类:

@Configuration
@ImportResource(value = {"classpath:beans.xml","classpath:applicationContext.xml"})

public class SystemConfig {
 //使用@Bean 声明 bean
 }

创建测试方法:

@Test
public void test04(){
	//没有 xml 配置文件,使用 java 类代替 xml 配置文件 的作用
	 ApplicationContext ctx = new AnnotationConfigApplicationContext(SystemConfig.class);
	 Cat cat = (Cat) ctx.getBean("myCat");
	 System.out.println("cat==="+cat);










}

5 @PropertyResource

@PropertyResource 是读取 properties 属性配置文件

在 resources 目录下创建 config.properties

tiger.name=东北老虎
tiger.age=6

创建数据类 Tiger

@Component("tiger")
public class Tiger {
	 @Value("${tiger.name}")
	 private String name
	 @Value("${tiger.age}")
	 private Integer age;
	 @Override
	 public String toString() {
		return "Tiger{" +
				"name='" + name + '\'' +
				", age=" + age +
				'}';

}}




修改 SystemConfig 文件

@Configuration
@PropertySource(value = "classpath:config.properties")
@ComponentScan(value = "com.bjpowernode.vo")
public class SystemConfig {
		//使用@Bean 声明 bean
		}




创建测试方法

@Test
public void test05(){
	//没有 xml 配置文件,使用 java 类代替 xml 配置文件 的作用
	ApplicationContext ctx = new AnnotationConfigApplicationContext(SystemConfig.class);
	Tiger tiger = (Tiger) ctx.getBean("tiger");
	System.out.println("Tiger==="+tiger);
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值