Springboot
1.springboot = spring + springmvc
2.常用框架配置好了
3.开发效率高
JavaConfig
是spring提供的Java类配置容器,配置springIOC容器的纯Java方法。
两个注解
1.@Configuration
放在类的上面,表示这个类作为配置文件使用
2.@Bean
声明对象,把这个对象注入到容器之中。
案例:
创建student类:
package com.gizzard.vo;
public class Student {
private String name;
private Integer age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
}
使用beans.xml方法:
在resources里面创建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标签-->
<bean id="myStudent" class="com.gizzard.vo.Student">
<property name="name" value="李四"/>
<property name="age" value="20"/>
<property name="sex" value="男"/>
</bean>
</beans>
测试:
@Test
public void test01(){
String config="beans.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
Student student = (Student) ctx.getBean("myStudent");
System.out.println(student);
}
使用JavaConfig方式:
创建SpringConfig类
package com.gizzard.config;
import com.gizzard.vo.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Configuration:表示当前类是作为xml配置文件使用的,就是用来配置容器的
* 位置:在类的上面
*
* SpringConfig这个类就相当于beans.xml
*/
@Configuration
public class SpringConfig {
/**
* 创建方法 方法的返回值是对象。在方法的上面加入@Bean
* 方法的返回值对象就注入到容器中。
*
* @Bean:把对象注入到spring容器中。作用相当于<bean>
*
* 位置:放在方法的上面
*/
@Bean
public Student createStudent() {
Student s1 = new Student ();
s1.setName("张三");
s1.setAge(22);
s1.setSex("男");
return s1;
}
@Bean(name = "lisiStudent")
public Student makeStudent() {
Student s1 = new Student ();
s1.setName("lisi");
s1.setAge(22);
s1.setSex("男");
return s1;
}
}
测试:
@Test
public void test02(){
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
Student student = (Student) ctx.getBean("createStudent");
System.out.println(student);
}
@Test
public void test03(){
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
Student student = (Student) ctx.getBean("lisiStudent");
System.out.println(student);
}
@ImporResource
@ImporResource
在当作为配置文件的类中加入该注解可以将配置文件中的bean也加入到该类之中。
@Configuration
@ImportResource(value = "classpath:applicationContext.xml")
public class SpringConfig {
/**
* 创建方法 方法的返回值是对象。在方法的上面加入@Bean
* 方法的返回值对象就注入到容器中。
*
* @Bean:把对象注入到spring容器中。作用相当于<bean>
*
* 位置:放在方法的上面
*/
@Bean
public Student createStudent() {
Student s1 = new Student ();
s1.setName("张三");
s1.setAge(22);
s1.setSex("男");
return s1;
}
@Bean(name = "lisiStudent")
public Student makeStudent() {
Student s1 = new Student ();
s1.setName("lisi");
s1.setAge(22);
s1.setSex("男");
return s1;
}
}