TestController类的代码:
package anocation.Controller;
import org.springframework.stereotype.Controller;
@Controller("TestController")
public class TestController {
public void testPrint(){
System.out.println("setPrint()");
}
}
UserTestObject类的代码:
package anocation;
import org.springframework.stereotype.Component;
@Component("UserTestObject")
public class UserTestObject {
public void test(){
System.out.println("test()");
}
}
UserService类的代码:
package anocation.Service;
import org.springframework.stereotype.Service;
@Service("UserService")
public class UserService {
public void testService(){
System.out.println("testService()");
}
}
UserRepository接口的代码:
package anocation.Repository;
public interface UserRepository {
public void add();
}
UserRepositoryImpl类的代码:
package anocation.Repository;
import org.springframework.stereotype.Repository;
@Repository("UserRepositoryImpl")
public class UserRepositoryImpl implements UserRepository {
@Override
public void add() {
System.out.println("add()");
}
}
Spring-config.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd" >
<context:component-scan
base-package="anocation">
</context:component-scan>
</beans>
Main类的代码:
package anocation;
import anocation.Controller.TestController;
import anocation.Repository.UserRepository;
import anocation.Service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext clt = new ClassPathXmlApplicationContext("spring-config.xml");
UserTestObject userTestObject = (UserTestObject) clt.getBean("UserTestObject");
System.out.println(userTestObject);
TestController testController = (TestController) clt.getBean("TestController");
System.out.println(testController);
UserRepository userRepository = (UserRepository) clt.getBean("UserRepositoryImpl");
System.out.println(userRepository);
UserService userService = (UserService) clt.getBean("UserService");
System.out.println(userService);
((ClassPathXmlApplicationContext) clt).close();
}
}
输出的结果:
九月 08, 2019 11:54:37 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-config.xml]
anocation.UserTestObject@a74868d
九月 08, 2019 11:54:39 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
anocation.Controller.TestController@12c8a2c0
anocation.Repository.UserRepositoryImpl@7e0e6aa2
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@b81eda8: startup date [Sun Sep 08 23:54:37 CST 2019]; root of context hierarchy
anocation.Service.UserService@365185bd
Process finished with exit code 0