- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道有数十亿人对这个问题提出疑问,但我无法解决我的问题。我有 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/
#include int main() { double a,b; scanf("%lf%lf",&a,&b); printf("%lf %lf",a,b); retu
我有一个网格(6 行,5 列): grid = [ [None, None, None, None, None], [None, None, None, None, N
我的 CSV 文件由制表符分隔,我想过滤掉那些所有条目(16 列)为 0 值的行。我现在正在做这个 awk '$1 != 0 && $2 != 0 && ....omitted && $16 != 0
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我的 Xcode 调试器报告,除了 all the IBOutlets 发现 nil(myLine 等)外,所有其他值包括 alive(bool 值)都已设置。顺便说一句,当我删除 App Deleg
posenet的初始化 const net = await posenet.load(); const pose = await net.estimateSinglePose(videoElement
所以下面的代码会发出两次错误警报: window.onload = function(){ alert(window.myframe.myarray insta
所以下面的代码会发出两次错误警报: window.onload = function(){ alert(window.myframe.myarray insta
我是一名优秀的程序员,十分优秀!