2016-12-20 55 views
0

我知道那個問題有數十億個問題,但我無法解決我的問題。我有Spring + Hibernate的應用程序。 Hibernate有2個配置(用於客戶和管理數據庫)。客戶的休眠使用DATABASE方法支持多租戶。另外我使用Spring Security。
這是我的租戶ID解析類:Autowired @Service不是無處不在

@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; 
    } 
} 

這是我的UserService:

@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); 
    } 
} 

我的問題:爲什麼UserServiceMyTenantIdResolver總是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屬於不同的休眠配置。

修訂
客戶休眠配置:

@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; 
}} 

聯繫冬眠配置:

@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; 
}} 

的Web應用程序配置:

@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; 
}} 
+0

提供您的配置類/文件。 –

+0

是的,你的配置文件。無論如何只是一個想法:單個服務如何在同一時間擁有兩個持久性conf? (如果我瞭解你的情況暴露) – Zorglube

+0

我的配置添加。 –

回答

2

這不可能是真的! @Autowire將強制該字段爲非空。所以userService=null唯一的情況是MyTenantIdentifierResolver不是春豆!

MyTenantIdentifierResolver的實例設置爲spring-bean。

+0

對不起,你的意思是添加@Bean註釋? –

+0

@ArtemP。不,班級配置得很好。搜索spring-configuration來找出爲什麼這個類沒有被用作spring bean。 –