<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--读取配置文件扫描的路径-->
<context:component-scan base-package="org.spring"/>
</beans>
@Component
public class Hello {
}
public class Application {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("my.xml");
Hello hello = context.getBean("hello", Hello.class);
System.out.println(hello);
}
}
注解注入
@Controller(value = "goto")
public class Go {
public void go(){
System.out.println("go///");
}
}
按类型和对象名称注入
@Component
public class Hello {
@Autowired
@Qualifier("goto")
public Go go;
}