当我想在Spring 3中的Session范围中使用模型时,我使用了foll.控制器中的注释: -
@SessionAttribute("myModel");
Run Code Online (Sandbox Code Playgroud)
但是,这只是myModel的声明.它在什么时候被初始化,以便我在View中使用它.Spring如何知道这个模型的类型?
有人可以用例子解释一下吗?
我有一个带有junit4测试套件的Spring 3.1 MVC + Hibernate 3.6项目.我的问题是我的测试用例中没有开始交易,甚至认为我添加了@Transactional.
我的测试用例调用了一个控制器和一个dao.在控制器中,无论如何都要启动一个事务,所以它不会抱怨.在dao中,我添加了一个@Transactional(propagation = Propagation.MANDATORY)以确保它将进行测试的事务.目前它引发了一个IllegalTransactionStateException,我想这意味着没有当前的事务.
我试图创建programmaticaly一个事务,它确实工作,这意味着获取dao服务的AOP代理不是问题的原因.但是我想用@Transactional注释创建一个事务.
这是我的道:
// ...imports...
@Repository("taskDao")
@Transactional(propagation = Propagation.MANDATORY)
public class TaskHome implements TaskDao {
private static final Log log = LogFactory.getLog(TaskHome.class);
private SessionFactory sessionFactory;
@Autowired
public TaskHome(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Task findById(int id) {
log.debug("getting Task instance with id: " + id);
try {
Task instance = (Task) this.sessionFactory.getCurrentSession().get(
Task.class, id); // exception raised here!
if (instance == null) …Run Code Online (Sandbox Code Playgroud) junit spring hibernate spring-annotations spring-transactions
当我尝试在 Oracle DB 上使用 spring 注释为 REPEATABLE_READ 设置隔离级别时,我得到:
java.sql.SQLException: READ_COMMITTED 和 SERIALIZABLE 是唯一有效的事务级别。- (ORA-17030)
这是处理这种情况的方法吗?
@Transactional(isolation = Isolation.REPEATABLE_READ)
@Override
public List<ToMq> getAndChange1000NotForwarded() {
String queryText = "SELECT c FROM ToMq c WHERE c.forwarded = 0 AND";
TypedQuery<ToMq> query = em.createQuery(queryText, ToMq.class);
query.setMaxResults(1000);
List<ToMq> resultList = query.getResultList();
Date current = new Date();
for (ToMq toMq : resultList) {
toMq.setForwarded(Boolean.TRUE);
toMq.setForwardTimestamp(current);
}
save(resultList);
return resultList;
}
Run Code Online (Sandbox Code Playgroud) 我想在我的 portlet 应用程序中使用 Ehcache。如果我想从缓存中删除数据,最好使用@CacheEvictor @TriggersRemove?
根据文档,@CacheEvict和@TriggersRemove注释看起来非常相似。
我在我的spring上下文xml中有以下xml配置,我使用基于注释的方法非常少,无法弄清楚如何使用注释来表示以下内容,需要帮助.
<bean id="myPolicyAdmin" class="org.springframework.security.oauth2.client.OAuth2RestTemplate">
<constructor-arg>
<bean class="org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails">
<property name="accessTokenUri" value="${accessTokenEndpointUrl}" />
<property name="clientId" value="${clientId}" />
<property name="clientSecret" value="${clientSecret}" />
<property name="username" value="${policyAdminUserName}" />
<property name="password" value="${policyAdminUserPassword}" />
</bean>
</constructor-arg>
</bean>
Run Code Online (Sandbox Code Playgroud)
在我的java类(策略管理器)中,它被称为如下,我实际上是在引用一个示例并尝试将其转换为所有注释.
@Autowired
@Qualifier("myPolicyAdmin")
private OAuth2RestTemplate myPolicyAdminTemplate;
Run Code Online (Sandbox Code Playgroud)
编辑:我尝试创建一个bean,org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails但不知道如何设置其属性以及如何作为构造函数args访问它myPolicyAdminTemplate
我的控制器中有这个方法:
@RequestMapping(method = RequestMethod.POST)
InterfaceClass insert(@RequestBody InterfaceClass interfaceClass) {
// Do something
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误非常简单且不言自明:
Can not construct instance of InterfaceClass: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information.
Run Code Online (Sandbox Code Playgroud)
基本上,我要告诉Spring,我有一个具体的实现InterfaceClass,ClassImpl。
我试过:
@JsonRootName("InterfaceClass")
public class ClassImpl implements InterfaceClass {
}
Run Code Online (Sandbox Code Playgroud)
没有任何程度。我不能使用@JsonTypeInfo 作为父接口的类InterfaceClass不应该知道ClassImpl并且它们在不同的模块中。我也尝试过:
实施InterfaceClass抽象AbstractClass和认沽:
@JsonDeserialize(as = AbstractClass.class)
Run Code Online (Sandbox Code Playgroud)
在InterfaceClass. 然后,延长AbstractClass使用ClassImpl。错误只是变成:
Can not construct instance …Run Code Online (Sandbox Code Playgroud) 是否可以@Value从另一个变量设置
例如.
System properties : firstvariable=hello ,secondvariable=world
@Value(#{systemProperties['firstvariable'])
String var1;
Run Code Online (Sandbox Code Playgroud)
现在我想要var2与var1它连接并依赖它,就像这样
@Value( var1 + #{systemProperties['secondvariable']
String var2;
public void message(){ System.out.printlng("Message : " + var2 );
Run Code Online (Sandbox Code Playgroud) spring spring-annotations spring-data spring-boot spring-config
我正在使用一个简单的 spring 应用程序来检查 @Configuration 和 @Bean(仅限基于 java 的配置),该程序可以同时使用 @Configuration 和不使用它。所以是否有必要拥有它。
这是我的代码,
学生.java
package com.cg.spring;
public class Student {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
}
Run Code Online (Sandbox Code Playgroud)
教师.java
package com.cg.spring;
public class …Run Code Online (Sandbox Code Playgroud) 我在 Spring Boot 中创建了一个验证器组件,并在application.properties. 我已经使用@Value注释来获取组件中正则表达式的值,并且我正在任何方法或构造函数之外编译模式,这给了我空指针异常,因为正则表达式当时没有获取它的值。但是当我将模式转移到某种方法时,它工作得很好。这是为什么?为什么即使使用 @Component 创建对象,@Value 也不起作用
看下面的代码:
返回 NullPointerException 的代码:
@Component
public class ValidString implements ConstraintValidator<ValidString, String> {
@Value("${user.input.regex}")
private String USER_INPUT_REGEX;
private Pattern USER_INPUT_PATTERN = Pattern.compile(USER_INPUT_REGEX);
@Override
public boolean validate(String userInput, ConstraintValidatorContext constraintValidatorContext) {
return USER_INPUT_PATTERN.matcher(userInput).find();
}
}
Run Code Online (Sandbox Code Playgroud)
代码工作正常:
@Component
public class ValidString implements ConstraintValidator<ValidString, String> {
@Value("${user.input.regex}")
private String USER_INPUT_REGEX;
private Pattern USER_INPUT_PATTERN;
@Override
public boolean validate(String userInput, ConstraintValidatorContext constraintValidatorContext) {
USER_INPUT_PATTERN = Pattern.compile(USER_INPUT_REGEX);
return USER_INPUT_PATTERN.matcher(userInput).find();
}
}
Run Code Online (Sandbox Code Playgroud)
另外,如果您能解释为什么第一个不起作用而第二个起作用,那就太好了。
应用程序属性
user.input.regex = …Run Code Online (Sandbox Code Playgroud) 我正在使用Spring注释和hibernate来创建DAO.我使用的是Spring 3.2.5和hibernate 4.3.3.出于某种原因,我一直得到这个例外:
org.hibernate.HibernateException:如果没有活动事务,则get无效
这是我的春季应用程序上下文的相关部分:
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Pick up the HibernateHouseDAO spring bean -->
<context:component-scan base-package="jinvestor.jhouse.download" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>jinvestor.jhouse.core.HouseEntity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<!-- <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop> -->
</props>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${dataSource.driver}" />
<property name="url" value="${dataSource.url}" />
<property name="username" value="${dataSource.username}" />
<property name="password" value="${dataSource.password}" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
Run Code Online (Sandbox Code Playgroud)
这是我的DAO:
@Repository("dao")
@Profile("mysql")
@Transactional
public class …Run Code Online (Sandbox Code Playgroud) spring ×7
java ×5
spring-boot ×3
hibernate ×2
spring-mvc ×2
annotations ×1
caching ×1
ehcache ×1
jpa ×1
junit ×1
oracle11g ×1
spring-cache ×1
spring-data ×1