快速入门
目录结构
程序流程
User是实体类;将User注入到UserInit中,初始化实体类;将UserInit注入到UserService中,调用UserInit的方法获得初始化后的User。需要注入的属性必须要有getter,setter方法
User实体类
package me.spring.model;
public class User {
int id;
String name;
int age;
String sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
UserInit
package me.spring.init;
import me.spring.model.User;
public class UserInit {
User user;
public User initUser(){
user.setAge(18);
user.setId(1);
user.setName("阿德勒");
user.setSex("男");
return user;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
UserService
package me.spring.service;
import com.alibaba.fastjson.JSON;
import me.spring.init.UserInit;
public class UserService {
UserInit userInit;
public void printUser(){
System.out.println(JSON.toJSONString(userInit.initUser()));
}
public UserInit getUserInit() {
return userInit;
}
public void setUserInit(UserInit userInit) {
this.userInit = userInit;
}
}
bean.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="user" class="me.spring.model.User" />
<bean id="userInit" class="me.spring.init.UserInit" >
<!-- 注入User -->
<property name="user" ref="user" />
</bean>
<bean id="userService" class="me.spring.service.UserService" >
<!-- 注入UserInit -->
<property name="userInit" >
<ref bean="userInit"/>
</property>
</bean>
</beans>
spring.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">
<!-- 引入beansx.xml,当有多个xml时这么写比较好 -->
<import resource="beans.xml"/>
</beans>
UserServiceTest
package me.spring.service;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.ConcurrencyThrottleSupport;
import me.spring.MyConfig;
import me.spring.service.UserService;
public class UserServiceTest {
static ApplicationContext context=null;
@BeforeClass
public static void B(){
context=new ClassPathXmlApplicationContext("resources/spring.xml");
//还可以用java类来管理bean之间的关系,MyConfig代码在下面
//context=new AnnotationConfigApplicationContext(MyConfig.class);
}
@Test
public void test() {
UserService s=(UserService) context.getBean("userService");
s.printUser();
}
}
MyConfig
package me.spring;
import me.spring.init.UserInit;
import me.spring.model.User;
import me.spring.service.UserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig {
@Bean(name="user")
public User user(){
return new User();
}
@Bean(name="userInit")
public UserInit userInit(){
UserInit userInit=new UserInit();
userInit.setUser(user());
return userInit;
}
@Bean(name="userService")
public UserService userService(){
UserService userService=new UserService();
userService.setUserInit(userInit());
return userService;
}
}