在spring以前,我们在开发程序时,所有的对象都是要自己一个个new出来的,或者通过工厂模式使用对象工厂一个个创建出来的。比如我们有这么几个类,一个是UserController,另一个是UserService接口,第三个是实现了UserService接口的类UserServiceImpl,UserController里包含了一个实现了UserService接口的成员对象:
public class UserController {
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public String sayHello(String to) {
return userService.sayHello(to);
}
}
public interface UserService {
String sayHello(String to);
}
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String to) {
return "hello " + to;
}
}
public class Test{
public static void main(String[] args) {
UserController uerController = new UserController();
UserService userService = new UserServiceImpl();
userController.setUserService(userService);
System.out.println(userController.sayHello("tom"));
}
}
public class Test{
public static void main(String[] args) {
Container container = Container.scanAndCreateContainer();
UserController uerController = container.getObject(UserController.class);
System.out.println(userController.sayHello("tom"));
}
}
4.0.0
com.mystudy
spring-demo-xml
1.0-SNAPSHOT
1.8
org.springframework
spring-context
4.3.10.RELEASE
org.apache.maven.plugins
maven-compiler-plugin
${java.version}
package com.mystudy;
import com.mystudy.controller.UserController;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml"); // 1
UserController userController = (UserController) context.getBean("userController"); //2
System.out.println(userController.sayHello("tom")); //3
}
}
package com.mystudy.controller;
import com.mystudy.service.UserService;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {
@Autowired
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public String sayHello(String to) {
return userService.sayHello(to);
}
}
package com.mystudy.service.impl;
import com.mystudy.service.UserService;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService{
@Override
public String sayHello(String to) {
return "hello " + to;
}
}
package com.mystudy.configure;
import com.mystudy.controller.UserController;
import com.mystudy.service.UserService;
import com.mystudy.service.impl.UserServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SpringConfig {
@Bean
public UserController userController() {
UserController userController = new UserController();
userController.setUserService(userService());
return userController;
}
@Bean
public UserService userService() {
return new UserServiceImpl();
}
}
package com.mystudy;
import com.mystudy.configure.SpringConfig;
import com.mystudy.controller.UserController;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MyTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); // 1
UserController userController = (UserController) context.getBean("userController");
System.out.println(userController.sayHello("tom"));
}
}
package com.mystudy.configure;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(value = {"com.mystudy"})
public class SpringConfig {
}