具体情况如下:
Caused by: java.lang.ClassNotFoundException: ${jdbc.driverClass}
而通过Controller中调用可以正常打印,由此可知是@Value加载的太慢了。

经过百度了好久,终于找到原因,由于Spring 和Shiro整合时,生命周期有某种冲突,需在spirng配置类中修改以下代码即可。(程序使用纯java配置无xml)

1
2
3
4
5
6
7
8
9
10
// 不加static,@Value加载properties文件太慢导致为null
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
// shiro 生命周期函数,不加static,@Value加载properties文件太慢导致为null
@Bean
public static LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
return new LifecycleBeanPostProcessor();
}

Spring完整配置类如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
@Configuration
@ComponentScan(value = "zone.lxy", excludeFilters = @ComponentScan.Filter(Controller.class)) //除了controller注解其他都扫
@PropertySource("classpath:jdbc.properties")
@EnableTransactionManagement // 开启事务控制
@MapperScan("zone.lxy.dao")
public class SpringConfig {
@Value("${jdbc.driverClass}")
private String driverClass;

@Value("${jdbc.jdbcUrl}")
private String jdbcUrl;

@Value("${jdbc.user}")
private String user;

@Value("${jdbc.password}")
private String password;


// 数据源
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
try {
dataSource.setDriverClassName(driverClass);
// dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl(jdbcUrl);
// dataSource.setUrl("jdbc:mysql://localhost:3306/emp?characterEncoding=utf8&useSSL=false");
dataSource.setUsername(user);
// dataSource.setUsername("root");
dataSource.setPassword(password);
// dataSource.setPassword("123456");
} catch (Exception e) {
e.printStackTrace();
}
return dataSource;
}

// 事务管理器
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}

// 配置mybatis-plus的bean工厂
@Bean
public MybatisSqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
// mybatis-plus分页插件
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
sqlSessionFactoryBean.setPlugins(paginationInterceptor);
return sqlSessionFactoryBean;
}

// shiro filter工厂
@Bean("shiroFilter")
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(securityManager);
Map<String, String> map = new LinkedHashMap<>();
map.put("/css/**", "anon");
map.put("/js/**", "anon");
map.put("/img/**", "anon");
map.put("/login", "anon");
map.put("/register", "anon");
map.put("/code", "anon");
map.put("/logout", "logout");
map.put("/**", "authc");
shiroFilterFactoryBean.setLoginUrl("/login");
shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
return shiroFilterFactoryBean;
}

// shiro web安全管理器
@Bean
public DefaultWebSecurityManager securityManager(CustomerRealm customerRealm) {
DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
defaultWebSecurityManager.setRealm(customerRealm);
return defaultWebSecurityManager;
}

// 不加static,@Value加载properties文件太慢导致为null
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
// shiro 生命周期函数,不加static,@Value加载properties文件太慢导致为null
@Bean
public static LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
return new LifecycleBeanPostProcessor();
}

// 开启shiro注解
@Bean
@DependsOn("lifecycleBeanPostProcessor")
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
return new DefaultAdvisorAutoProxyCreator();
}
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
advisor.setSecurityManager(securityManager);
return advisor;
}
}

__END__

三国小梦
文章作者:三国小梦
文章出处spring整合shiro后@Value加载变慢导致变量值为null
作者签名:简单地活着, 肆意又精彩.
关于主题Hexo - Live For Code
版权声明:文章除特别声明外,均采用 BY-NC-SA 许可协议,转载请注明出处