来自Spring @Transactional属性是否适用于私有方法?
使用代理时,应仅将@Transactional注释应用于具有公共可见性的方法.如果使用@Transactional注释对带保护的,私有的或包可见的方法进行注释,则不会引发错误,但带注释的方法不会显示已配置的事务设置.
我可以想出排除private和package-private方法的充分理由,但为什么protected方法不会在事务上表现?以下stacktrace显示公共方法的正确行为(通过接口代理调用):
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_51]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) ~[spring-aop-4.2.1.RELEASE.jar:4.2.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring-aop-4.2.1.RELEASE.jar:4.2.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.2.1.RELEASE.jar:4.2.1.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.2.1.RELEASE.jar:4.2.1.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) ~[spring-tx-4.2.1.RELEASE.jar:4.2.1.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.2.1.RELEASE.jar:4.2.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.1.RELEASE.jar:4.2.1.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) ~[spring-aop-4.2.1.RELEASE.jar:4.2.1.RELEASE]
at com.sun.proxy.$Proxy145.improveType(Unknown Source) ~[na:na]
Run Code Online (Sandbox Code Playgroud)
当调用"相同"的受保护方法(通过非接口CGLIB代理)时,我们得到以下内容:
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_51]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) ~[spring-aop-4.2.1.RELEASE.jar:4.2.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring-aop-4.2.1.RELEASE.jar:4.2.1.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720) ~[spring-aop-4.2.1.RELEASE.jar:4.2.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.2.1.RELEASE.jar:4.2.1.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653) ~[spring-aop-4.2.1.RELEASE.jar:4.2.1.RELEASE]
at my.company.webservices.facade.EntityFacade$$EnhancerBySpringCGLIB$$fd77735b.findEntity(<generated>) ~[spring-core-4.2.1.RELEASE.jar:na]
Run Code Online (Sandbox Code Playgroud)
这显然是一个设计决定(为什么?),但我认为它显然是一个开发人员错误,它无声地失败.
编辑
当使用接口(接口中只有公共方法)时,这显然不是问题,但由于Spring不一定需要通过CGLIB代理对象的接口,因此调用受保护的@Transactional方法将表现得像公共方法(即通过代理),除了按设计它忽略了事务性.
我想使用@FeignClient(url=...)并使其直接转到给定的 url,而不是从功能区配置中获取主机。
我知道在 spring-cloud feign 中默认与ribbon和eureka一起出现。
根据这个:https : //cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-ribbon.html#spring-cloud-ribbon-without-eureka 可以禁用尤里卡并提供硬编码列表功能区的主机,例如:
${serviceId}:
ribbon:
listOfServers: ${host}
Run Code Online (Sandbox Code Playgroud)
根据这个:https : //cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html 可以为 feign 提供一个显式的 url,例如:
@FeignClient(name = "${feign.name}", url = "${feign.url}")
public interface StoreClient {
//..
}
Run Code Online (Sandbox Code Playgroud)
所以我对 FeignClient 到底会使用什么感到有些困惑:提供的 url 或来自功能区配置的主机?
我希望一些道具也可以禁用功能区以进行伪装,因为如果给 feign 一个单一的 url,那么负载平衡有什么意义?
Spring-cloud 发布火车 - Camden.SR2
我的一位同事最近在 eclipse 中创建了一个新项目,并使用内置的 git 客户端提交和推送。在我克隆到我的计算机并用 eclipse 打开后,我发现 eclipse 创建了 .classpath 文件。.classpath 不是 eclipse 项目(也是 .project)查找引用的 jar 的关键文件吗?谷歌搜索后我很困惑,看到所有讨论都在谈论忽略它们。它们对 Eclipse 正常工作不是很重要吗?为什么人们忽视他们?如果我没有忽略它们,有什么问题?
我正在尝试分析一些旧代码,以了解在发生错误时它正在做什么.这是功能:
error = -11;
protected String decodeError(int error){
StringBuffer msg = new StringBuffer();
for(int j=1; j<ERR_16+1; j=j*2 ){
if( (error&j)==j ){
switch(j){
case ERR_1:
msg.append("error1");
break;
case ERR_4:
msg.append("error4");
break;
case ERR_8:
msg.append("error8");
break;
case ERR_16:
msg.append("error16");
break;
}
}
}
return msg.toString();
}
ERR_16 = 16;
ERR_2 = 2;
ERR_4 = 4;
...
Run Code Online (Sandbox Code Playgroud)
如果错误= -11,该函数返回ERR_4.我知道&运算符是按位AND运算符,但为什么有人应该在这种情况下使用它?
我的日志依赖项目前如下所示:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-bom</artifactId>
<version>2.9.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我注意到commons-logging.jar由于某种原因它仍然在我的类路径中。我应该排除它还是不会引起任何问题?
到目前为止我没有注意到任何问题,但我仍然想知道那个罐子是否仍然会以某种方式引起问题。
我刚刚完成了section1,并注意到没有介绍如何将项目推到数组的特定位置的方法。例如,如果我想显示数组
var suits = ["hearts","clubs","Brooks Brothers", "diamonds","spades"]
Run Code Online (Sandbox Code Playgroud)
如何将“ Brooks Brothers”推入阵列服的位置[2],并将其余的1向下移?javascript中是否有类似于push的内置函数,使我能够做到这一点?
我想我总是可以有些困难:
function add (item, position){
var length = suits.length;
for(i = length -1; i >= position; i--){
suits[length] = suits[i];
length--;
};
suits[position] = item;
};
add("Brooks Brothers",2) //to add it to the middle
Run Code Online (Sandbox Code Playgroud) java nio select 的代码和linux epoll 的代码看起来是一样的。没有循环获取套接字 fd 之类的东西,linux 选择代码片段确实使用循环获取套接字 fd。
所以我的问题是,java nio select 和 linux epoll 的方式是一样的吗?
java nio选择
while (true) {
try {
selector.select();
Set<SelectionKey> selectionKeys = selector.selectedKeys();
selectionKeys.forEach((selectionKey) -> {
final SocketChannel client;
try {
if (selectionKey.isAcceptable()) {
ServerSocketChannel server = (ServerSocketChannel) selectionKey.channel();
client = server.accept();
client.configureBlocking(false);
client.register(selector, SelectionKey.OP_READ);
String key = "[" + UUID.randomUUID().toString() + "]";
clientMap.put(key, client);
} else if (selectionKey.isReadable()) {
client = (SocketChannel) selectionKey.channel();
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
int count = client.read(readBuffer);
if (count …Run Code Online (Sandbox Code Playgroud) 我知道这char[]比String密码更可取,因为它String是不可变的。但是我一直无法弄清楚如何将它传递给准备好的语句。考虑这个代码:
Connection con = MyDBManager.connect();
PreparedStatement stm = con.prepareStatement(
"INSERT INTO my_table(username, password) VALUES(? ?)");
String username = "Jonny";
stm.setString(username);
char[] password = new char[] {'a','b','c','d'};
stm.SetString(password.toString());
Run Code Online (Sandbox Code Playgroud)
它有效,但我怀疑调用会password.toString()破坏使用 a 的全部目的char[],那么我该怎么办?
是的,我知道密码不应该以明文形式存储。密码是散列的,但我仍然想使用char[]
这似乎有效,而且我还将数据库中的字段从 更改text为bytea并且(我认为)理论上应该提供与char[]预期相同的好处,但我强烈感到我做得不对:
byte[] password = new byte[] {'a','b','c','d'};
stm.SetBytes(password);
Run Code Online (Sandbox Code Playgroud)
也许我的方法完全错误,但我的要求是密码哈希永远不应存在于不可变对象中。我对将其发送到数据库的其他方式持开放态度PreparedStatement
我从@rzwitserloot 的回答中学到了很多东西,但不幸的是它没有帮助。永远不要在不可变对象中使用密码哈希是不可协商的要求。
在 Java 8default中引入了方法实现。我的问题是为什么需要default在方法名称/签名中有关键字。为什么不能default像通常的方法实现一样没有关键字?