@Controller
public class GoodsController {
@Autowired
private TestBean testBean;
public void save(){
ApplicationContext applicationContext = FactoryConfig.getApplicationContext();
GoodsController goodsController = (GoodsController)applicationContext.getBean("goodsController");
goodsController.testBean.test();
testBean.test();
}
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
GoodsController userController = new GoodsController();
userController.save();
}
}
@Service
public class TestBean {
public void test(){
System.out.println("这是testbean");
}
}
这是testbean
java.lang.NullPointerException
at com.test.mybatis.controller.GoodsController.save(GoodsController.java:30)
at com.test.mybatis.controller.GoodsController.main(GoodsController.java:34)
goodsController.testBean.test();
testBean.test();
你的testBean并没有实例化 为空
@Autowired
private TestBean testBean;
这个不是自动装配么 不需要实例化啊
goodsController.testBean.test();这个是比手动 建立的实体类 确实存在了,
testBean.test()你是打算用注入得到的实体类,
但是用这个的前提是,程序执行了你在xml文件中配置的注解扫描、只有注解扫描了,才会把注解的实体类放到容器中,以供依赖注入。
出现空指正异常,说明你的注解没扫描!
<?xml version="1.0" encoding="UTF-8"?>
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.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-3.0.xsd">
<!-- 加载数据库配置 -->
<context:property-placeholder location="classpath:mysql.properties" />
<!-- 数据库配置 有3种 推荐使用当前 可以加入连接池配置 当前未加入 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="${jdbc.driver}">
</property>
<property name ="url"
value="${jdbc.url}"><!-- jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=UTF-8 -->
</property>
<property name="username" value="${jdbc.username}">
</property>
<property name="password" value="${jdbc.password}">
</property>
</bean>
<!-- 集成mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis配置文件 -->
<property name="configLocation" value="classpath:/spring-mybatis.cfg.xml" />
<!-- 加载xml 映射 配置文件 -->
<property name="mapperLocations" value="classpath:/com/test/mybatis/mapping/**/*.xml"/>
<!-- 自动配置别名 -->
<property name="typeAliasesPackage" value="com.test.mybatis.entity" />
</bean>
<!-- 扫描basePackage下所有以@MyBatisDao注解的接口 -->
<!--
-->
<!-- <bean id="testBean" class="com.test.mybatis.controller.TestBean"></bean> -->
<!-- <bean id="goodsDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.test.mybatis.dao.GoodsDao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean> -->
<!-- 开启自动扫描 可以自动注入 bean -->
<context:component-scan base-package="com" />
<!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!-- <context:annotation-config /> --><!-- 手动注入的bean -->
帮我看看差了什么么 扫描我是加入的