当前位置: 代码迷 >> 综合 >> Could not autowire field: private java.lang.String cn.com.service.Regedit.name;
  详细解决方案

Could not autowire field: private java.lang.String cn.com.service.Regedit.name;

热度:52   发布时间:2023-11-19 21:25:57.0

错误代码

@Autowired
private SessionFactory sf;
@Autowired
private String name;

 错误原因:

@Autowired是自动装配的意思

 

1、如果你装配的是对象类型的如

@Autowired
private SessionFactory sf;

@Autowired
private Car c;

你要在applciationContext里面配置好

<bean name="car" class="cn.com.bean.Car">
<property name="name" ></property>
</bean>

或者是扫描包的路径包含这个对象的路径

<context:component-scan base-package="cn.com.*"></context:component-scan>


2、如果你装配的是

    @Autowired
private String name;

就大错特错了,这个String是基础类型,需要提供set和get方法

我对这个自动装配不理解,以为只要是属性都可以装配!