@Autowired注解不起作用,求指点

喝可乐怕蛀牙 2017-04-06 04:51:50
package com.yangwan.beans;

import org.springframework.beans.factory.annotation.Autowired;

public class AutoWirePro {

@Autowired
private Car car;
@Autowired
private Bicycle bicycle;
public void printAutoWireProInformation(){
car.printCarInformation();
bicycle.printBicycleInformation();
}
}


配置文件代码
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.yangwan.beans" />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!-- 利用构造函数带参数的索引式方法注入bean
<bean id="car" class="com.yangwan.beans.Car">
<constructor-arg index="0" value="大众"></constructor-arg>
<constructor-arg index="1" value="88888"></constructor-arg>
</bean>
-->
<bean id="autowirepro" class="com.yangwan.beans.AutoWirePro"/>
<bean id="car" class="com.yangwan.beans.Car">
<constructor-arg type="double" value="333233"></constructor-arg>
<constructor-arg type="String" value="大众"></constructor-arg>
</bean>
<bean id="bicycle" class="com.yangwan.beans.Bicycle">
<property name="brand" value="Ferfly"></property>
<property name="price" value="389.4"></property>
</bean>
<bean id="carstaticfactory" class="com.yangwan.beans.CarFactory" factory-method="getInstance">
<constructor-arg index="0" value="奥迪"></constructor-arg>
<constructor-arg index="1" value="78872"></constructor-arg>
</bean>
<!-- 定义实例工厂 -->
<bean id="bicycleinstancefactory" class="com.yangwan.beans.BicycleInstanceFactory"></bean>
<!-- 利用实例工厂创建bean -->
<bean id="bicycleinstance" factory-bean="bicycleinstancefactory" factory-method="getInstance">
<constructor-arg index="0" value="HariErfi"></constructor-arg>
<constructor-arg index="1" value="899"></constructor-arg>
</bean>
<bean id="requiredEntity" class="com.yangwan.beans.RequiredEntity">
</bean>
</beans>


测试代码:
public class Main {
public static void main(String[] args) {
Resource resource = new FileSystemResource("src/main/resources/spring/application-config.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
Car tempCar = beanFactory.getBean("car",Car.class);
tempCar.printCarInformation();
Bicycle bicycle = beanFactory.getBean("bicycle",Bicycle.class);
bicycle.printBicycleInformation();
RequiredEntity requiredEntity = beanFactory.getBean("requiredEntity",RequiredEntity.class);
requiredEntity.printEntityInformation();
AutoWirePro autoWirePro = beanFactory.getBean("autowirepro",AutoWirePro.class);
autoWirePro.printAutoWireProInformation();
}

}

输出结果:
brand: 大众 price: 333233.0
brand: Ferfly price: 389.4
name : null
number: 0
Exception in thread "main" java.lang.NullPointerException
at com.yangwan.beans.AutoWirePro.printAutoWireProInformation(AutoWirePro.java:12)
at com.yangwan.test.Main.main(Main.java:28)

原因是car和bicycle对象没有被实例化都是null.感觉是配置问题。包括加上
<context:annotation-config />
<context:component-scan base-package="com.yangwan.beans" />
等,均没有效果。
...全文
1612 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
喝可乐怕蛀牙 2017-04-12
  • 打赏
  • 举报
回复
1楼的方法是正确的,我之前使用Resource获得配置文件,然后根据resource来创建BeanFactory,根据BeanFactory依据id从ioc容器获取beans实例。代码表面上看没有错,但是假如@Autowire注解后,老是发现注解不起作用,IOC容器并不能自动创建实例。 Resource resource = new FileSystemResource("配置文件路径"); BeanFactory beanFactory = new XmlBeanFactory(resource); Bean bean = beanFacotory.getBean("bean_id",Bean.class); 改用1楼的的方法,使用ApplicationContext来获取IOC容器返回beans实例,这个问题获得解决。 String[] locations = {"file:C:/Users/bw/Desktop/yw/AutowireTestPro/src/main/resources/spring/application-config.xml"}; ApplicationContext ctx = new ClassPathXmlApplicationContext(locations); Bean bean = (Bean)ctx.getBean("bean_id"); 感谢。不过具体原因还不是很清楚,还有就是ClassPathXmlApplicationContext中classpath的相对路径我还是不清楚,我的工程结构是maven的,还在研究!
953384951 2017-04-10
  • 打赏
  • 举报
回复
楼主,你既然用扫描的方法,那你干嘛不在实体类上注释你要的值呢?非得要xml文件上配置?
podd 2017-04-07
  • 打赏
  • 举报
回复
引用 8 楼 vkqiang 的回复:
要有实体类,加上get,set
这是我很不负责任的一次回答
vkqiang 2017-04-07
  • 打赏
  • 举报
回复
要有实体类,加上get,set
明悟 2017-04-07
  • 打赏
  • 举报
回复
不能对同一个bean 实例化用配置式,属性注入用注解式
明悟 2017-04-07
  • 打赏
  • 举报
回复
你在类AutoWirePro 上加@Service注解,把配置文件的 <bean id="autowirepro" class="com.yangwan.beans.AutoWirePro"/> 去掉就没问题了
X元素 2017-04-07
  • 打赏
  • 举报
回复
@Autowired 是不需要get set的, @Autowired private Car car; @Autowired private Bicycle bicycle; 这两个bean对象是需要声明成bean的。实体对象上加上@Service 试试
podd 2017-04-07
  • 打赏
  • 举报
回复
要有实体类,加上get,set
_jant 2017-04-07
  • 打赏
  • 举报
回复
把get set方法加上就可以了
userLiangjunjie 2017-04-07
  • 打赏
  • 举报
回复
有提供get,set方法么
李德胜1995 2017-04-06
  • 打赏
  • 举报
回复
这样写吧

ApplicationContext app=new ClassPathXmlApplicationContext("classpath:xml文件路径名称");
		System.out.println(app==null);
		AutoWirePro car=app.getBean(AutoWirePro.class);
		car.printAutoWireProInformation();

​ 博主介绍:✌在职Java研发工程师、专注于程序设计、源码分享、技术交流、专注于Java技术领域和毕业设计✌项目名称基于Web的酒店客房管理系统的设计与实现系统说明3.2.1  用户模块功能分析前台模块应主要包括用户登录模块、用户注册模块、查看客房信息模块、客房预定 模块、用户留言模块、充值模块和个人信息维护模块。用户登录模块:用户通过输入注册的的账号和密码,然后进行身份验证,匹配成功 后实现登录功能。用户注册模块:用户输入合法的账号和密码可以实现注册功能。 查看客房信息模块:用户可以通过首页查看客房的价格,图片,详情等信息,从而   可以选择想要预定的房间。 客房预定模块:用户选择自己想要预定的客房后,可以通过输入相关信息进行订房操作。用户留言模块:用户可以向管理员发送留言。 充值模块:用户可以通过添加银行卡再为自己进行充值操作。 个人信息维护模块:用户可以修改自己的姓名、密码、身份证号等信息,还可以查看自己的订单。3.2.2  管理员模块功能分析对于管理员而言,一个好的管理系统总是能让酒店的管理工作事半功倍[7]。管理员 能够通过这个系统对自己的酒店情况一目了然,应该包括客户留言模块、客房管理模 块、订房信息管理模块、入住信息管理模块、统计分析模块、酒店新闻管理模块、会 员信息管理模块、员工信息管理模块、系统用户管理模块、个人信息维护模块。客户留言模块:管理员可以查看并回复用户的留言。客房管理模块:管理员可以管理客房信息,可以添加新的客房,删除已经停用的客房信息,还可以修改现有的客房信息。订房信息管理模块:管理员可以处理用户的订房请,为用户办理入住手续。入住信息管理模块:对于没有注册的线下客户,也可以办理入住手续。统计分析模块:可以对酒店所有的入住信息进行记录和总结分析。酒店新闻管理模块:管理员可以更新网站上的新闻公告,展示图片等信息。会员信息管理模块:管理员可以查看到所有的注册会员信息,可以对会员信息进行删除,修改,添加操作。员工信息管理模块:管理员可以查看到自己公司所有的员工信息,而且还可以对员工的相关信息进行管理。系统用户管理模块:管理员可以查看到所有的系统管理员信息并对管理员信息进行管理。个人信息维护模块:管理员可以更改自己的登录密码或者是姓名、性别、手机号等 相关个人信息。​编辑 环境需要1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS; 5.数据库:MySql 5.7版本;6.是否Maven项目:否;技术栈1. 后端:Spring+SpringMVC+Mybatis2. 前端:JSP+CSS+JavaScript+jQuery使用说明1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;3. 将项目中springmvc-servlet.xml配置文件中的数据库配置改为自己的配置;4. 运行项目,在浏览器中输入http://localhost:8080/ 登录运行截图​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑​编辑 用户管理控制层:package com.houserss.controller;import javax.servlet.http.HttpSession;import org.apache.commons.lang3.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import com.houserss.common.Const;import com.houserss.common.Const.Role;import com.houserss.common.ServerResponse;import com.houserss.pojo.User;import com.houserss.service.IUserService;import com.houserss.service.impl.UserServiceImpl;import com.houserss.util.MD5Util;import com.houserss.util.TimeUtils;import com.houserss.vo.DeleteHouseVo;import com.houserss.vo.PageInfoVo;/** * Created by admin */@Controller@RequestMapping(/user/)public class UserController if (ip != null && ip.length() > 0) String[] ips = ip.split(,);for (int i = 0; i  

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧