我使用SpringBoot的@Value注释从默认的application.properties文件填充对象属性,但在Filter覆盖中看到一些奇怪的行为。
我的调试器中有两个断点:
@Component
public class BasicFilter implements Filter {
@Value("${aad.client_id}")
private String clientId;
@Bean
public FilterRegistrationBean registerFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new BasicFilter()); // <-- 1.
registration.addUrlPatterns("/secure/*");
return registration;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (request instanceof HttpServletRequest) { // <- 2.
HttpServletRequest httpRequest = (HttpServletRequest) request;
...
1:this.client_id
设为&#39; foo&#39; (来自application.properties
A2 2:this.client_id
为空
对不同/缺失值的任何解释?
答案 0 :(得分:4)
您正在return new DefaultTabController(
length: 2,
child: new Scaffold(
body: new NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
new SliverAppBar(
forceElevated: innerBoxIsScrolled,
floating: true,
//elevation: elevation,
pinned: true,
expandedHeight: 250.0,
bottom: new TabBar(
tabs: <Tab>[
new Tab(
text: 'green',
icon: new Icon(Icons.show_chart),
),
new Tab(
text: 'purple',
icon: new Icon(Icons.history),
),
],
),
flexibleSpace: new FlexibleSpaceBar(
title: new FarsiText(
"آقای نوری",
style: titleTextStyle,
),
centerTitle: true,
background: new Container(
decoration: new BoxDecoration(
gradient: new LinearGradient(
begin: const Alignment(0.5, -0.5),
end: const Alignment(-1.5, 1.5),
colors: <Color>[
Colors.purple[400],
Colors.lightBlue[100],
],
),
),
padding: new EdgeInsets.only(
top: screenWidth / 4,
bottom: screenWidth / 7,
),
child: new Column(
children: <Widget>[
new Stack(
overflow: Overflow.visible,
children: <Widget>[
new CircleAvatar2(
boxShadow: new BoxShadow(
color: Colors.white70,
spreadRadius: 0.0,
blurRadius: 50.0,
),
radius: 40.0,
backgroundImage:
const AssetImage("assets/images/noori.jpg"),
),
new Positioned(
child: new Container(
padding: const EdgeInsets.all(3.0),
decoration: new BoxDecoration(
borderRadius: new BorderRadius.all(
const Radius.circular(8.0),
),
border: new Border.all(
color: Colors.white,
width: 1.5,
),
),
child: new FarsiText(
"تایید شده",
color: Colors.white,
fontSize: 15.0,
),
),
left: -100.0,
),
],
),
],
),
),
),
leading: getAppBarLeading(context),
),
];
},
body: new TabBarView(
children: <Widget>[
new Center(
child: new Container(
height: 1000.0,
color: Colors.green.shade200,
child: new Center(
child: new FlutterLogo(colors: Colors.green),
),
),
),
new Center(
child: new Container(
height: 1000.0,
color: Colors.purple.shade200,
child: new Center(
child: new FlutterLogo(colors: Colors.purple),
),
),
),
],
),
),
),
);
注入new BasicFilter()
。在这种情况下,您正在创建一个新对象,但它不会是一个Spring托管bean,因此它的FilterRegistrationBean
将为空。所以基本上你现在有2个实例:
clientId
注释@Component
关键字注入FilterRegistrationBean
的一个(不是由Spring管理)您应该将new
方法移动到单独的配置类(使用registerFilter()
注释,并将@Configuration)
bean自动注入/注入该方法,例如:
BasicFilter
另外,如果要将@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean registerFilter(BasicFilter filter) { // Inject it
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(filter); // Use it
registration.addUrlPatterns("/secure/*");
return registration;
}
}
方法保留在过滤器本身中,则应将其替换为@Bean
,因为当前对象由Spring管理,而新实例赢得了“{1}}”。是:
this