Spring全注解开发

1、用配置类代替配置文件

@Configuration
@ComponentScan("com.jixian")
public class SpringConfig {

}

原来的配置文件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"
       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.xsd">
    <context:component-scan base-package="com.jixian"/>

</beans>

2、测试

        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
        UserControlller userControlller = applicationContext.getBean(UserControlller.class);
        userControlller.add();

 

posted @ 2023-02-26 19:57  诸葛卧龙仙人  阅读(26)  评论(0)    收藏  举报