gpt4 book ai didi

java - Autowired @Service 并非在任何地方都为 null

转载 作者:行者123 更新时间:2023-11-30 02:42:27 26 4
gpt4 key购买 nike

我知道有数十亿人对这个问题提出疑问,但我无法解决我的问题。我有 Spring + Hibernate 的应用程序。 Hibernate 有 2 个配置(用于客户数据库和管理数据库)。客户的 hibernate 支持使用数据库方法的 Multi-Tenancy 。我还使用 Spring Security。
这是我的租户 ID 解析类:

@Component
public class MyTenantIdentifierResolver implements CurrentTenantIdentifierResolver {

@Autowired
private UserService userService;

public String resolveCurrentTenantIdentifier() {
User u = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
com.lspsystem.model.User us = userService.getByLogin(u.getUsername());
return us.getCompany().getTenantId();
}

public boolean validateExistingCurrentSessions() {
return true;
}
}

这是我的用户服务:

@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserDAO userDAO;

public User get(Integer id) {
return userDAO.get(id);
}

public boolean delete(Integer id) {
return userDAO.delete(id);
}

public void update(User u) {
userDAO.update(u);
}

public List<User> getAll(User u) {
return userDAO.getAllUsers();
}

public User getByLogin(String login) {
return userDAO.getByLogin(login);
}
}

我的问题:为什么 MyTenantIdResolver 中的 UserService 始终为 null?但是,例如在下一类(来自安全性)中,它不会 null

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

@Autowired
private UserService userService;

public User loadUserByUsername(String s) throws UsernameNotFoundException {
com.lspsystem.model.User u = userService.getByLogin(s);
return new User(u.getEmail(), u.getPassword(), true, true, true, true, getGrantedAuthorities(u));
}

@Autowired
public UserDetailsServiceImpl(UserService userService) {
this.userService = userService;
}

private List<GrantedAuthority> getGrantedAuthorities(com.lspsystem.model.User u) {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
return authorities;
}
}

请注意,UserService 和 MyTenantIdResolver 属于不同的 hibernate 配置。

已更新
客户 hibernate 配置:

@Configuration
@EnableTransactionManagement
@PropertySource(value = {"classpath:customerHibernate.properties"})
public class RemoteDBHibernateConfig {

@Autowired
private Environment environment;

@Bean(name = "customerSessionFactory")
public SessionFactory sessionFactory() throws IOException{
LocalSessionFactoryBean builder =
new LocalSessionFactoryBean();
builder.setDataSource(dataSource1());
builder.setPackagesToScan("com.lspsystem.model");
builder.setHibernateProperties(hibernateProperties());
builder.afterPropertiesSet();

return builder.getObject();
}

@Bean("defaultDS")
public DataSource dataSource1() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}

private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
properties.put("hibernate.multiTenancy", environment.getRequiredProperty("hibernate.multiTenancy"));
properties.put("hibernate.multi_tenant_connection_provider", environment.getRequiredProperty("hibernate.multi_tenant_connection_provider"));
properties.put("hibernate.tenant_identifier_resolver", environment.getRequiredProperty("hibernate.tenant_identifier_resolver"));
return properties;
}

@Bean
@Autowired
public HibernateTransactionManager transactionManager() throws IOException{
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory());
return txManager;
}}

管理 hibernate 配置:

@Configuration
@EnableTransactionManagement
@PropertySource(value = {"classpath:adminHibernate.properties"})
public class AdminDBHibernateConfig {

@Autowired
private Environment environment;

@Bean(name = "adminSessionFactory")
public SessionFactory sessionFactory() throws IOException {
LocalSessionFactoryBean builder =
new LocalSessionFactoryBean();
builder.setDataSource(dataSource());
builder.setPackagesToScan("com.lspsystem.model");
builder.setHibernateProperties(hibernateProperties());
builder.afterPropertiesSet();

return builder.getObject();
}

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("admin.jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("admin.jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("admin.jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("admin.jdbc.password"));
return dataSource;
}

private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("admin.hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("admin.hibernate.show_sql"));
properties.put("hibernate.format_sql", environment.getRequiredProperty("admin.hibernate.format_sql"));
properties.put("hibernate.hbm2dll.auto", environment.getRequiredProperty("admin.hibernate.hbm2ddl.auto"));
return properties;
}

@Bean
@Autowired
public HibernateTransactionManager transactionManager() throws IOException{
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory());
return txManager;
}}

网络应用配置:

@Configuration
@EnableWebMvc
@Import(SecurityConfiguration.class)
public class WebApplicationConfig extends WebMvcConfigurerAdapter{

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

@Bean
public InternalResourceViewResolver viewResolver(){
InternalResourceViewResolver view = new InternalResourceViewResolver();
view.setViewClass(JstlView.class);
view.setPrefix("/WEB-INF/jsp/");
view.setSuffix(".jsp");
return view;
}}

UserDAO,例如:

@Repository
public class UserDAOImpl implements UserDAO {

@Autowired
@Qualifier("adminSessionFactory")
private SessionFactory sessionFactory;

public com.lspsystem.model.User get(Integer id) {
Session session = sessionFactory.openSession();
User u = (User)session.load(User.class, id);
return u;
}

public User getByLogin(String login) {
Session session = sessionFactory.openSession();
Query q = session.createQuery("SELECT u FROM User u WHERE u.email = :email");
q.setParameter("email", login);
User u = (User)q.list().get(0);
return u;
}}

最佳答案

这不可能是真的! @Autowire 将强制该字段为非空。因此,唯一具有 userService=null 的情况是当 MyTenantIdentifierResolver 不是 spring bean 时!

MyTenantIdentifierResolver 实例设置为 spring-bean。

关于java - Autowired @Service 并非在任何地方都为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41241057/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com