spring基于注解的基础使用(仅限使用逻辑)
1.在spring.xml文件中加入扫描包的代码
<context:component-scan base-package="huang.test.dao"></context:component-scan>
2.在需要加入容器的类上加上各类注解(如果不加属性,则默认类名首字母小写为bean名字)
@Component("dao")
public class Dao implements IDao {
public void method1(){
System.out.println("dao.method1");
}
}
3.在需要使用容器的类中通过xml路径加载容器并使用
public class Client {
public static void main(String[] args) {
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("spring.xml");
IDao dao = applicationContext.getBean("dao", IDao.class);
dao.method1();
}
}