我正在尝试创建一个通道拦截器,它将一些数据添加到消息的标题中,并可能检查 MDC(映射诊断上下文)中是否有信息,尽管第二部分是我还没有开始弄清楚的。
对于第一部分,我正在阅读 spring 集成的文档并说:
请记住,receive() 调用仅与 PollableChannels 相关。事实上, SubscribableChannel 接口甚至没有定义 receive() 方法。这样做的原因是当消息被发送到 SubscribableChannel 时,它将根据频道的类型直接发送给一个或多个订阅者(例如 PublishSubscribeChannel 发送给它的所有订阅者)。因此,preReceive(..) 和 postReceive(..) 拦截器方法仅在拦截器应用于 PollableChannel 时调用。
看到拦截器需要实现的接口:
public interface ChannelInterceptor {
Message<?> preSend(Message<?> message, MessageChannel channel);
void postSend(Message<?> message, MessageChannel channel, boolean sent);
void afterSendCompletion(Message<?> message, MessageChannel channel, boolean sent, Exception ex);
boolean preReceive(MessageChannel channel);
Message<?> postReceive(Message<?> message, MessageChannel channel);
void afterReceiveCompletion(Message<?> message, MessageChannel channel, Exception ex);
}
Run Code Online (Sandbox Code Playgroud)
我想知道所有这些方法中的哪一个是我应该拦截消息的更安全的地方?由于其中一些无法执行,具体取决于 Channel 类型。例如,像 SubscribableChannel 一样,它不会执行接收,所以我假设 preReceive 和 postReceive 永远不会被调用。
基于此,我可以假设 preSend 是一个更安全的地方吗?
我是Spring AOP的新手.
使用基于注释的Spring配置:
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass=true)
@ComponentScan({"sk.lkrnac"})
Run Code Online (Sandbox Code Playgroud)
方面:
@Aspect
@Component
public class TestAspect {
@Before("execution(* *(..))")
public void logJoinPoint(JoinPoint joinPoint){
....
}
}
Run Code Online (Sandbox Code Playgroud)
弹簧组件:
package sk.lkrnac.testaop;
@Component
public class TestComponent{
@PostConstruct
public void init(){
testMethod();
}
public void testMethod() {
return;
}
}
Run Code Online (Sandbox Code Playgroud)
如何拦截Spring框架本身调用的所有公共方法?(例如,Spring创建TestComponent实例时的TestComponent.init())目前我只能TestComponent.testMethod()通过调用来拦截:
TestComponent testComponent = springContext.getBean(TestComponent.class);
testComponent.testMethod();
Run Code Online (Sandbox Code Playgroud) 我正在学习 Spring Core 认证,我对如何正确回答这个问题有些疑问:
\n\n\n\n\n如何在 Spring 中配置数据源?哪种 bean 对于开发非常有用?
\n
我认为我可以这样做来在 Spring XML 配置文件中配置DataSource :
\n\n<bean id=\xe2\x80\x9cdataSource\xe2\x80\x9d class=\xe2\x80\x9corg.apache.commons.dbcp.BasicDataSource\xe2\x80\x9d>\n <property name=\xe2\x80\x9curl\xe2\x80\x9d value=\xe2\x80\x9c${dataSource.url}\xe2\x80\x9d />\n <property name=\xe2\x80\x9cusername\xe2\x80\x9d value=\xe2\x80\x9c${dataSource.username}\xe2\x80\x9d />\n <property name=\xe2\x80\x9cpassword\xe2\x80\x9d value=\xe2\x80\x9c${dataSource.password}\xe2\x80\x9d />\n</bean>\n\n<jdbc:initialize-database data-source=\xe2\x80\x9cdataSource\xe2\x80\x9d>\n <jdbc:script location=\xe2\x80\x9cclasspath:schema.sql\xe2\x80\x9d />\n <jdbc:script location=\xe2\x80\x9cclasspath:test-data.sql\xe2\x80\x9d />\n</jdbc:initialize-database>\nRun Code Online (Sandbox Code Playgroud)\n\n所以我认为答案 bean 是org.apache.commons.dbcp.BasicDataSource。这个断言是真的还是我错过了什么?
\n\n到底代表什么配置标签?我很清楚它的作用,但到底代表什么?这是一个特殊的 bean 声明还是什么?
\n\n总氮
\n例如我有处理程序:
@Component
public class MyHandler {
@AutoWired
private MyDependency myDependency;
@Value("${some.count}")
private int someCount;
public int someMethod(){
if (someCount > 2) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
测试它我写了以下测试:
@RunWith(MockitoJUnitRunner.class}
class MyHandlerTest {
@InjectMocks
MyHandler myHandler;
@Mock
MyDependency myDependency;
@Test
public void testSomeMethod(){
ReflectionTestUtils.setField(myHandler, "someCount", 4);
myHandler.someMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
我可以使用模拟变量someCount ReflectionTestUtils.我能以某种方式使用Mockito注释来模拟它吗?
我正在用 Spring 开发一个 angularjs 应用程序。
我经常不得不更改我的 html/javascript 文件,我注意到 spring 正在缓存静态内容。我怎样才能禁用它?
我已经尝试过这个...
@Configuration
@AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
class WebMvcConfig extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter {
@Autowired
private Environment env;
@Bean
public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
return new ResourceUrlEncodingFilter();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
boolean devMode = this.env.acceptsProfiles("dev");
//boolean useResourceCache = !devMode;
boolean useResourceCache = false;
Integer cachePeriod = devMode ? 0 : null;
registry.addResourceHandler("/public/**")
.addResourceLocations("/public/", "classpath:/public/")
.setCachePeriod(cachePeriod)
.resourceChain(useResourceCache)
.addResolver(new GzipResourceResolver())
.addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
.addTransformer(new AppCacheManifestTransformer());
}
}
Run Code Online (Sandbox Code Playgroud)
然后 ...
WebContentInterceptor webContentInterceptor;
public @Bean WebContentInterceptor webContentInterceptor () { …Run Code Online (Sandbox Code Playgroud) 这个问题可以解决很多一般性的解决方案,所以我将尝试用这种方式来表达它。我有一个情况我需要同样beforeEach和afterEach代码运行,但需要的块来改变只是一个变量(或几个)各一次。
我的特定情况:我需要每次从数据库中插入/删除。码:
首先设置临时表进行测试:
before(function(done) {
db.query('CREATE TEMPORARY TABLE tmp AS SELECT * FROM example_table', null, function(error) {
return done();
});
});
Run Code Online (Sandbox Code Playgroud)
然后在每次测试之前/之后进行钩子[使用@luboskrnac建议进行编辑]:
var vals = []
beforeEach(init_vals, function(done) {
mysql.query('INSERT INTO tmp (a, b) VALUES (?, ?)', init_vals, function(error, rows) {
return done();
});
});
// pass values [1, 2] to `beforeEach`
it('check value is in database', function(done) {
mysql.query('SELECT * FROM tmp WHERE a = ? AND b = ?', function(error, rows) { …Run Code Online (Sandbox Code Playgroud) 我有一个使用 Struts2 用 Java 编写的 Web 服务,struts 控制器返回 jsp 页面,现在我想使用 React 组件重写前端,最好的开始方式是什么?
在spring项目中,我想LocalDate从@Autowired构造函数参数创建一个值,该参数的值在.properties文件中。我想做两件事:
my.date,则应通过解析属性值来创建参数设置属性后,以及使用以下命令时:
@DateTimeFormat(pattern = "yyyy-MM-dd") @Value("${my.date}") LocalDate myDate,
...
Run Code Online (Sandbox Code Playgroud)
我收到此错误:java.lang.IllegalStateException:无法将类型“ java.lang.String”的值转换为所需类型“ java.time.LocalDate”:找不到匹配的编辑器或转换策略
我还使用iso = ...来使用具有相同结果的ISO日期。
LocalDate.now()我尝试使用默认值,例如:
@Value("${my.date:#{T(java.time.LocalDate).now()}}") LocalDate myDate,
...
Run Code Online (Sandbox Code Playgroud)
但是我得到了同样的错误。
原谅我对Spring的无知,但是如何在这里实现两个目标?
我正在尝试在以下方法中运行entityManager.merge(myEntity),但似乎忽略了@Transactional注释.Hibernate配置似乎很好,因为我可以成功地从数据库中获取数据,但是无法写入数据库.我使用的是Spring 3.2.3版.为什么编写db操作不起作用?
我的方法不起作用
package com.reflections.importer.bls;
...
@Service
class BlsGovImporter {
...
@Transactional
private void importSeries(String externalId) {
// This works. The dao is using EntityManager too
Series series = seriesDao.findByExternalId(externalId);
series.getValues().addAll(fetchNewValues());
// This does not work and no exception is thrown
entityManager.merge(series);
}
Run Code Online (Sandbox Code Playgroud) 当我在我的项目中包含spring-boot时,它具有其他spring组件的预定义版本.例如:spring-boot 1.3.5带spring-boot-starter-web自动包含spring-mvc 4.2.6.如果我想使用webmvc 4.3的一些功能,我怎么知道它们兼容?是否存在任何兼容性矩阵?或者我应该将它视为单个版本而不更改版本?
我在STS(Spring工具套装)上创建了Spring启动项目(Spring启动项目),包括Window和Mac.这是我的pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
Mac上缺少某些依赖关系层次结构.
示例:
冬眠验证器
我需要Mac上的jar而不修改pom.xml.我想我错过了一些环境Mac.
我2天前买了Mac.这是我的第一台Mac.
我正在尝试测试我的控制器建议异常处理。我已将我的控制器建议注册到我的 mockmvc:
mockMvc = MockMvcBuilders.standaloneSetup(controller)
.setControllerAdvice(new ExceptionHandlingControllerAdvice())
.build();
Run Code Online (Sandbox Code Playgroud)
我可以在控制台中看到测试正在选择异常处理方法:
org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver Detected @ExceptionHandler methods in com.myapp...ExceptionHandlingControllerAdvice
Run Code Online (Sandbox Code Playgroud)
ExceptionHandlingControllerAdvice 类有一个处理安全异常的方法:
@ExceptionHandler(SecurityException.class)
Run Code Online (Sandbox Code Playgroud)
当我的单元测试抛出 SecurityException 时,测试失败并显示堆栈跟踪,而不是调用控制器建议中的处理程序方法。
我做错了什么吗?
我的reducer中的代码如下:
import {ADD_FILTER, REMOVE_FILTER} from "../../../../actions/types";
const removeFilter = (state, name) => {
return state.filter(f => f.name !== name);
};
export default function addRemoveFilterReducer(state = [], action) {
switch (action.type) {
case ADD_FILTER:
let nState = state;
if(state.some(i => i.name === action.searchFilter.name))
nState = removeFilter(state, action.searchFilter.name);
return [...nState, {name: action.searchFilter.name, values: [action.searchFilter.values]}];
//Call another action
case REMOVE_FILTER:
return removeFilter(state, action.searchFilter.name);
default:
return state;
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个组件陈列室,在展厅内我有搜索组件和内容组件.
在搜索组件里面,我处理过滤,然后我发送一个用这个reducer处理的动作.
添加过滤器后,我想分配一个包含所有过滤器的操作.我怎样才能做到这一点?
该操作将使用reducer处理,我只返回符合搜索条件的汽车并将其显示在内容组件中.
我希望你明白我想说的话.
这种方法有用吗?
java ×11
spring ×10
spring-boot ×4
javascript ×3
spring-mvc ×3
reactjs ×2
unit-testing ×2
aop ×1
autowired ×1
dependencies ×1
hibernate ×1
jdbc ×1
mocha.js ×1
mocking ×1
mockito ×1
react-redux ×1
redux ×1
spring-jdbc ×1
spring-test ×1
struts2 ×1
templating ×1
validation ×1