spring半注解开发helloworld

<?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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。