一个简单的Spring应用程序

步骤

1  新建java工程

2  Spring相关的jar包放到lib目录下

3  写一个测试bean

4  写配置文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
       default-lazy-init="true">
       
    <bean id="test" class="abc.Test"></bean>
</beans>

5  写main函数

        ApplicationContext context = new FileSystemXmlApplicationContext("classpath:config.xml");
        Test test = (Test)context.getBean("test");
        System.out.println(test.getName());

2018-09-14

Spring自动搜bean的配置

    <!-- 使用注解式注入 -->
    <context:annotation-config />
    <!-- 自动扫描 -->
    <context:component-scan base-package="cn.angelshelter.*" />

还有一个很奇怪的现象:我的类在cn.angelshelter.spring_study.Student

如果我的base-package设为cn.angelshelter.spring_study.*它是找不到Student的,只有设为cn.angelshelter.*才找得到。可能String认为那个*号只能代码包名(package),不能代表类名吧(class)。

 

posted on 2012-10-21 21:42  angelshelter  阅读(201)  评论(0编辑  收藏  举报

导航