关于static自动注入的问题

zhanggc1001 2014-06-04 08:55:41
本人对spring的注入不太了解


@Component
public class UserLdap {

public static final String BASE_DN = "ou=InnerUsers,ou=Users";

private static LdapTemplate ldapTemplate;

@Autowired(required = true)
public UserLdap(@Qualifier("ldapTemplate")LdapTemplate ldapTemplate) {
UserLdap.ldapTemplate = ldapTemplate;
}
}


spring配置文件


<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
<property name="url" value="xxxxxxxx" />
<property name="userDn" value="xxxxxxxx" />
<property name="password" value="xxxxxxxx" />
</bean>

<bean name="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="contextSource" />
</bean>


在使用ldapTemplate的时候始终是null

像这样的静态类怎样才能自动注入进来呢!!!

...全文
539 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhanggc1001 2014-06-05
  • 打赏
  • 举报
回复
引用 2 楼 defonds 的回复:
ldapTemplate 的初始化需要 contextSource,而你没有传入。UserLdap 的构造子传入 contextSource
LdapTemplate 的构造子是contextSource啊 UserLdap 的构造子是ldapTemplate 所以感觉你说的有点没明白,呵呵
不想低调 2014-06-05
  • 打赏
  • 举报
回复
好高深的设计问题!
Defonds 2014-06-05
  • 打赏
  • 举报
回复
ldapTemplate 的初始化需要 contextSource,而你没有传入。UserLdap 的构造子传入 contextSource
zhanggc1001 2014-06-04
  • 打赏
  • 举报
回复
spring的xml文件里还有这么一个东西

	<context:annotation-config />
	<context:component-scan base-package="cn.gov.hbmsa" />
个人感觉是定义自动注入的扫描路径 不知道还有哪里不对呢

81,092

社区成员

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

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