小编Sha*_*yan的帖子

使用current_session_context_class属性hibernate 3 hibernate 4

我有一个应用程序,Spring和Hibernate3在生产中运行良好.以下是Spring的applicationContext.xml中的会话工厂的配置

       <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingDirectoryLocations">
        <list>
            <value>classpath:/hibernate</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect
            </prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.use_sql_comments">true</prop>
            <prop key="hibernate.max_fetch_depth">2</prop>
            <prop key="hibernate.autocommit">false</prop>
            <prop key="hibernate.current_session_context_class ">thread</prop>
                            <prop key="hibernate.generate_statistics">true</prop>
            <prop key="hibernate.jdbc.batch_size">20</prop>
        </props>
    </property>
</bean>

<bean id="txManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean 
    below) -->
<tx:advice id="txAdvice" transaction-manager="txManager">
    <!-- the transactional semantics... -->
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRED" />
        <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        <tx:method name="count*" …
Run Code Online (Sandbox Code Playgroud)

spring hibernate hibernate-session

10
推荐指数
1
解决办法
2万
查看次数

tomcat 7.0.50 java websocket实现给出了404错误

我正在尝试使用Java Websocket API(1.0) - JSR 356中指定的带注释端点在tomcat 7.0.50上实现websocket.以下是我编写代码的简要步骤1)使用@ServerEndpoint注释编写websocket端点2)实现@onOpen和@onMessage方法3)在谷歌浏览器上使用javascript打开websocket.

请按顺序查找与上述步骤对应的代码

1)步骤1和2 - 编写websocket服务器端点:

        package com.jkweb.websocket;

       import java.io.IOException;
       import java.util.HashMap;
       import java.util.List;
       import java.util.Map;

       import javax.websocket.EndpointConfig;
       import javax.websocket.OnMessage;
       import javax.websocket.OnOpen;
       import javax.websocket.Session;
       import javax.websocket.server.PathParam;
       import javax.websocket.server.ServerEndpoint;

       import org.slf4j.Logger;
       import org.slf4j.LoggerFactory;

  @ServerEndpoint(value="/websocket/fileuploadtracker")

@OnOpen 
public void open(Session session,EndpointConfig config) {
    ......
}
@OnMessage
public void onMessage(Session session, String msg) {
    try {
        session.getBasicRemote().sendText(msg);
    } catch (IOException e) {
        logger.error(e.getMessage());
    }
}

public static void sendMessage(String uniqueTocken,String msg){
    try {
        Session wsSession = socketConnectionMap.get(uniqueTocken);
        wsSession.getBasicRemote().sendText(msg);
    } …
Run Code Online (Sandbox Code Playgroud)

javascript java tomcat websocket

10
推荐指数
1
解决办法
1万
查看次数

如何同步访问javascript对象的私有成员

我有一个Javascript对象创建如下:

var ccStatTracker = (function (){
  ccmap:{
    "1":["1","2","3","4"],
    "2":["4","5"];       
  }

  return {
    modifyCCMap: function (){
      // Code which takes following actions: 
      // - adds/removes keys.
      // - modifies arrays stored as values against the keys in the map.  
    }  
  }
)();
Run Code Online (Sandbox Code Playgroud)

我有一个DHTMLXGrid组件,它以行和列的形式显示网格.当我编辑网格中的任何单元格时,会调用"onEditCell"事件.现在,我想从附加到"onEditCell"事件的事件处理函数调用ccStatTracker.modifyCCMap().当我继续修改单元格时,这个事件将被异步调用,这将调用一个函数"modifyCCMap",它将修改我的Javascript对象的私有成员"CCMap".因此,两次调用看到的CCMap的最新状态可能不同吗?那么处理这个问题的最佳方法是什么?Javascript和Java中有"同步"的东西吗?

请帮助我,因为它将决定我们为实现这一目标而采取的方法.

javascript synchronized members

7
推荐指数
1
解决办法
1万
查看次数

Servlet过滤器作为组件Spring Boot

我想在Spring Boot Web应用程序中配置Servlet过滤器,我想使用@Value注释自动装配一些Beans和属性.我能够使用以下配置实现此目的:

   @Configuration
   public class MyWebConfig{  
      @Autowire
      MyFilter filter;

      @Autowire
      MyAnotherFilter anotherFilter;  

      @Bean
      public FilterRegistrationBean someFilterRegistration() {
           FilterRegistrationBean registration = new  FilterRegistrationBean();
           registration.setFilter(filter);
           registration.setOrder(1);
           return registration;
      }

      @Bean
      public FilterRegistrationBean someFilterRegistration() {
           FilterRegistrationBean registration = new  FilterRegistrationBean();
           registration.setFilter(anotherFilter);
           registration.setOrder(2);
           return registration;
      }

   } 
Run Code Online (Sandbox Code Playgroud)

我已经配置了两个过滤器(为简洁起见只显示一个过滤器):

@Configuration
public class MyFilter implements Filter{

   @Value("${my.property.key}")
   private String myProperty;

   public void doFilter(...){
        ....
   }

   //init and destroy stubs  
        ....    
} 
Run Code Online (Sandbox Code Playgroud)

一切正常.我仍然有一些问题:
1)即使我注释掉FilterRegistrationBean代码片段也是如此.如果我想设置某个顺序,我觉得我必须使用FilterRegistrationBean.正确?
2)有没有什么方法可以设置订单或其他配置,如没有FilterRegistrationBean的网址模式?
3)我相信我可以使用@Component可以替换Filter类上的@Configuration注释,它会正常工作吗?
4)最后将Filter类本身标记为@ Component/@Configuration是否合适?

请注意,我在主应用程序类上使用@SpringBootApplication.

configuration spring components servlet-filters spring-boot

6
推荐指数
1
解决办法
6227
查看次数

如何使用Spring Security for REST API对用户进行身份验证

我一直在寻找这个问题的答案.但是,在阅读了包括Spring Security文档在内的大量资源之后,我仍然无法完全理解如何使用Spring Security对REST API的用户进行身份验证.
这是我想要做的:

  • 1)我想使用公钥 - 私钥实现像Amazon S3服务这样的身份验证
  • 2)为此,我将发送一个HMAC令牌,其中包含授权标头中的每个请求和公钥或我自己的自定义标头.
  • 3)我想使用spring security来实现令牌的服务器端处理,如下所示:
    • 1)从头部获取公钥.
    • 2)使用我的cutom UserDetailService类获取存储在数据库中的私钥
    • 3)使用请求和私钥计算服务器端的HMAC令牌,就像我在客户端上做的那样
    • 4)比较两个令牌以验证用户
    • 5)如果成功,则在SecurityContextHolder中存储认证对象.

从上面我不知道如何在Spring Security中执行此操作.我所理解的如下:

  • 1)使用<http>元素和自定义过滤器.我这样做了如下

    <http use-expressions="true" create-session="stateless" 
          authentication-manager-ref="restAuthenticationManager" 
          entry-point-ref="jkwebRestAuthenticationEntryPoint">
        <intercept-url pattern="/api/**" access="isAuthenticated()">
        </intercept-url>
        <custom-filter ref="jkWebSecurityHmacAuthenticationFilter"
         position="FORM_LOGIN_FILTER"/>
    </http>  
    
    Run Code Online (Sandbox Code Playgroud)
  • 2)现在我可以访问此过滤器中的标头并从中访问公钥.
    但是,我不知道如何使用此公钥从数据库中检索私钥.我是否需要使用自定义AuthenticationManager或AuthenticationProvider?以及如何做到这一点.我可以直接将我的UserDetailService Bean注入过滤器bean吗?

  • 3)如果我不需要使用自定义AuthenticationManager或AuthenticationProvider,我可以将UserDetailService直接注入过滤器bean,那么我是否需要自定义AuthenticationEntryPoint?
  • 4)假设我能够在过滤器中检索并进行身份验证,我是否需要调用chain.doFilter并将请求转发到相应的控制器?
  • 5)如果过滤器中的身份验证失败,我应该如何使用401响应客户端或自定义身份验证入口点?如果有,怎么样?
  • 6)我还需要将Authioities放在UserDetails中,我在UserDetailsS​​ervice实现中构造它并在UserDetails对象中设置它.只需在安全上下文中设置身份验证对象就可以完成工作,一旦身份验证成功,我们就会进行授权吗?

如果需要,我可以发布更多代码以便清晰.

authentication rest spring-mvc spring-security

5
推荐指数
0
解决办法
2084
查看次数

为什么实例方法的Java方法引用无法分配给Consumer接口

这是我的代码:

public class SearchByLambda {

     private Map<String,Consumer<Person>> searchCritertiaHolder = new HashMap<String,Consumer<Person>>();

     private static final String AGED = "aged";

     public SearchByLambda(){
           searchCritertiaHolder.put(AGED, (Person p)-> {p.filterAgedPerson(p);} );
     }

     private Consumer<Person> getFilter(String personType){
          return searchCritertiaHolder.get(personType);
     }

     public static void main(String[] args) {
          SearchByLambda searchUsage = new SearchByLambda();
          Person p = new Person(59,"shailesh");
          Person p1 = new Person(58,"ganesh");

          searchUsage.getFilter(AGED).accept(p);
          searchUsage.getFilter(AGED).accept(p1);

          Person.printAgedPersons();
     }
 }

 class Person{

       private static List<Person> agedPersons = new ArrayList<>();

       private int age;

       private String name;

       public int getAge() {
              return age; …
Run Code Online (Sandbox Code Playgroud)

java lambda

5
推荐指数
1
解决办法
1888
查看次数

NgRx createAction 方法返回类型签名的含义

我一直在通过 NgRx Doumentation for createAction 方法,如下面的链接: createAction 方法的重载

我无法理解下面这个方法的类型签名,特别是 createAction 方法的返回类型:什么是

() => TypedAction<T>
Run Code Online (Sandbox Code Playgroud)

在这个签名中:

 ActionCreator<T, () => TypedAction<T>>
Run Code Online (Sandbox Code Playgroud)

我没有看到任何参考TypedAction?它是否意味着任何特定动作类型的形状对象?

我在上面的返回类型签名中对 T 的理解是,它是 ActionCreator 函数的泛型类型,它将在调用时返回 T 类型的 Action。但不确定另一个 Type 参数表明它似乎是某个返回TypedActionT 类型的函数。想知道一个真实世界的例子。

generics typescript ngrx

4
推荐指数
1
解决办法
953
查看次数

Mockito没有正确地将列表作为参数存根

我正在尝试模拟一个类,并在调用模拟对象上的方法时返回一个存根的对象列表.让我们考虑以下代码:

interface MyRepositry{
       public List<MyClass> getMyClassInstances(String str,Long id,List<Integer> statusList);
}
Run Code Online (Sandbox Code Playgroud)

我在上面的方法ivocation嘲笑如下:

when(myRepository.getMyClassInstances("1234", 200L, stubbedList)).thenReturn(stubbedMyClassInstanceList);
Run Code Online (Sandbox Code Playgroud)

哪里

 stubbedList 
Run Code Online (Sandbox Code Playgroud)

我是通过插入两个整数1和3.在实际通话创建列表也是我通过我构建出在这里有整数1和3点OT笔记列表是stubbedList对象,并在实际通话清单对象是不同的,但总是包含两个整数1和3.

stubbedMyClassInstanceList    
Run Code Online (Sandbox Code Playgroud)

是MyClass实例的存根列表.

然而,当我运行测试时,mockito会返回一个空列表.我做了一些调试,我猜mockito无法匹配我正在使用的列表对象

      when(..).thenReturn(..)
Run Code Online (Sandbox Code Playgroud)

呼叫和实际呼叫,因此找不到正确的签名.

我不能用

anyList() 
Run Code Online (Sandbox Code Playgroud)

matcher因为我总是传递两个整数的列表(1和3).

我已经通过使用自定义解决了这个问题

     ArgumentMatcher 
Run Code Online (Sandbox Code Playgroud)

如下 :

     class StatusMatcher extends ArgumentMatcher<List> {
    public boolean matches(Object list) {
        List statuses = ((List) list);
        return (statuses.size() == 2 && statuses.contains(1) && statuses.contains(3));
    }
}
Run Code Online (Sandbox Code Playgroud)

所以问题是:

1)我猜测为什么短戳/嘲弄不正常?2)并且我使用的解决方案是正确的吗?

java unit-testing mockito stubbing argument-matcher

3
推荐指数
1
解决办法
5143
查看次数

当我尝试自动装配时,环境为空

我正在尝试使用 Spring 的 Environment 抽象和 @PropertySource 在我的 @Configuration 注释类中加载和使用属性。但是,我在 PropertyConfig 类中将 Environment 设置为 null,因为它是从另一个 @Configuration 类 PersistenceConfig 访问的,该类使用它来访问属性。这是我的相关代码:

   @Configuration
  @PropertySource({ "classpath:/properties/email_${environment}.properties" })
  @PropertySource({ "classpath:/properties/appconfig.properties" })
  @PropertySource({ "classpath:/properties/ApplicationResources.properties" })
  @PropertySource({ "classpath:/properties/Database_${environment}.properties" })
  @PropertySource({ "classpath:/properties/log4j.properties" })
  @PropertySource({ "classpath:/properties/system.properties" })
  public class PropertiesConfig {

        @Autowired
        private Environment env;

        private static final PropertiesAccessor propertyAccessor = new                   PropertiesConfig().new PropertiesAccessor();

        public static String getPopertyValue(String property){
            return propertyAccessor.getPropertyValue(property);
        }

        private class PropertiesAccessor{

        public String getPropertyValue(String key){
             return env.getProperty(key);
        }
    }
 }
Run Code Online (Sandbox Code Playgroud)

我的其他@Configuration 注释类 PersistenceConfig 如下:

  @Configuration
  @EnableTransactionManagement …
Run Code Online (Sandbox Code Playgroud)

environment configuration spring properties autowired

3
推荐指数
1
解决办法
9511
查看次数

ES6解构对象赋值函数参数默认值

嗨,我在这里通过函数参数Object Destructuring Demo经历了对象解构的使用示例

function drawES6Chart({size = 'big', cords = { x: 0, y: 0 }, radius = 25} = **{}**) {
  console.log(size, cords, radius);
 // do some chart drawing
}

 // In Firefox, default values for destructuring assignments are not yet  
 implemented (as described below). 
 // The workaround is to write the parameters in the following way:
   // ({size: size = 'big', cords: cords = { x: 0, y: 0 }, radius: radius =  
      25} = **{}**)

 drawES6Chart({ …
Run Code Online (Sandbox Code Playgroud)

javascript object destructuring ecmascript-6

3
推荐指数
3
解决办法
1713
查看次数

为什么不抛出ConcurrentModificationException

当您使用Java 1.5现代for循环迭代集合并删除一些元素并发抛出异常抛出时.

但是,当我运行以下代码时,它不会抛出任何异常:

    public static void main(String a []){
          Set<String> strs = new HashSet<String>();
          strs.add("one");
          strs.add("two");
          strs.add("three);

          for(String str : strs){
                   if(str.equalsIgnoreCase("two"){
                          strs.remove(str);
                   }
          }  
    }   
Run Code Online (Sandbox Code Playgroud)

上面的代码不会抛出ConcurrentModificationException.但是当我在我的Web应用程序服务方法中使用任何这样的for循环时,它总是抛出一个.为什么?我确信当它在服务方法中运行时没有两个线程正在访问集合那么是什么导致两个场景中的区别在于它被抛入一个而不是另一个?

java collections exception

1
推荐指数
1
解决办法
153
查看次数