2.pom.xml文件
- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <groupId>com.it.springbootgroupId>
- <artifactId>001-springboot-preartifactId>
- <version>1.0-SNAPSHOTversion>
-
-
- <dependencies>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>5.3.1version>
- dependency>
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
- dependency>
- dependencies>
-
- <build>
- <plugins>
-
- <plugin>
- <artifactId>maven-compiler-pluginartifactId>
-
- <version>3.5.1version>
-
- <configuration>
- <source>1.8source>
- <target>1.8target>
-
- <encoding>UTF-8encoding>
- configuration>
- plugin>
- plugins>
- build>
-
- project>
3.如果使用xml配置文件,则需要创建实体类,创建beans.xml配置文件
Student类
- package com.it.springboot.entity;
-
- public class Student {
- private String name;
- private Integer age;
- private String sex;
-
- @Override
- public String toString() {
- return "Student{" +
- "name='" + name + '\'' +
- ", age=" + age +
- ", sex='" + 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;
- }
- }
beans.xml配置文件
- "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="myStudent" class="com.it.springboot.entity.Student">
- <property name="name" value="张山"/>
- <property name="age" value="20"/>
- <property name="sex" value="男"/>
- bean>
-
- beans>
MyTest测试文件

测试结果:
4.使用JavaConfig配置文件
SpringConfig类
- package com.it.springboot.config;
-
- import com.it.springboot.entity.Student;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- /**
- * Configuration:表示当前类作为配置文件使用的,就是配置容器的。
- * 位置:在类的上面
- * SpringConfig这个类就相当于beans.xml
- */
- @Configuration
- public class SpringConfig {
- /**
- * 创建方法,方法的返回值是对象。在方法的上面加入@bean
- * 方法的返回值对象就注入到容器中。
- *
- * @Bean:把对象注入到spring容器中,作用相当于
- * 位置:方法的上面
- *
- * 说明:@Bean,不指定对象的名称,默认是方法名的id
- */
- @Bean
- public Student createStudent(){
- Student s1=new Student();
- s1.setName("李四");
- s1.setAge(19);
- s1.setSex("女");
- return s1;
- }
-
- /**
- * 指定对象在容器中的名字
- * @return
- */
- @Bean(name = "lisiStudent")
- public Student makeStudent(){
- Student s2=new Student();
- s2.setName("小红");
- s2.setAge(22);
- s2.setSex("女");
- return s2;
- }
-
- }
测试文件

测试结果

测试文件

测试结果
