FAQ Database Discussion Community


Cache is empty after setting up and using ehcache in Spring

java,spring,annotations,ehcache,spring-annotations
My code is below, when I get to the end and try to print out something from the cache, the key list is empty. @Configuration @EnableCaching public class EhcacheConfiguration implements CachingConfigurer { CacheConfiguration cacheConfiguration = new CacheConfiguration(); cacheConfiguration.setName("DataCache"); cacheConfiguration.setMemoryStoreEvictionPolicy("LRU"); cacheConfiguration.setMaxEntiresLocalHeap(1000); cacheConfiguration.setEternal(false); net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();...

@PreAuthorize and intercept-url priority

spring-security,spring-annotations
I have <security:http use-expressions="true"> <security:intercept-url pattern="/**/*" access="hasRole('ROLE_USER')"/> in the Spring Security context configuration file and @PreAuthorize("permitAll") @RequestMapping("/public") public String aMethod() { // ... } in a controller. What I want is that all the URLs to require authentication except public. Is this possible?...

How to inject a Map using the @Value Spring Annotation?

java,spring,dependency-injection,annotations,spring-annotations
How can i inject values into a Map from the properties file using the @Value annotation in Spring ? My Spring Java class is and i tried using the $ but, got the following error message Could not autowire field: private java.util.Map Test.standard; nested exception is java.lang.IllegalArgumentException: Could not resolve...

Timing Issue with Spring Boot Annotation Configuration

java,spring-boot,spring-annotations
We're having a curious issue with the timing of beans, repositories, and controllers in our spring boot application. We have a NodeRepository that is backed by a Map. This Map object should be a Map that we create using an @Bean annotation, but it seems as though Spring is creating...

Difference between @CacheEvict and @TriggersRemove annotations

caching,annotations,ehcache,spring-annotations,spring-cache
I want to use Ehcache in my portlet application. If I want to remove data from cache, it is better to use @CacheEvict or @TriggersRemove? According to documentation it looks like @CacheEvict and @TriggersRemove annotations are very similar....

REPEATABLE_READ on Oracle using JPA in Spring

java,jpa,oracle11g,spring-annotations
when I try to set isolation level using spring annotation to REPEATABLE_READ on Oracle DB I get: java.sql.SQLException: READ_COMMITTED and SERIALIZABLE are the only valid transaction levels. - (ORA-17030) It's a way to deal with this? @Transactional(isolation = Isolation.REPEATABLE_READ) @Override public List<ToMq> getAndChange1000NotForwarded() { String queryText = "SELECT c FROM...

Can not understand difference in behavior between : @EnableWebMvc and

java,spring,spring-annotations
I was looking for annotation based approach for <interceptors> and then found the way here So I used it and its working, but there is a catch. When I do; @Configuration @EnableWebMvc public class WebApplicationConfig extends WebMvcConfigurerAdapter{ .... } and there is no <annotation-driven /> tag in servlet-context.xml then it...

what is the order of bean loading if I have multiple configuration files in spring?

spring,spring-mvc,dependency-injection,spring-annotations
I have three configuration files in spring application. @Configuration public class FooConfig { ... } @Configuration public class BarConfig { ... } @Configuration public class FooBarConfig { ... } what is the order in which the beans are loaded? can I use a bean defined in FooConfig in BarConfig and...

Why does not “@Transactional(propagation = propagation.NEVER)” work?

java,spring,transactions,spring-aop,spring-annotations
On the spring docs, for never propagation: Execute non-transactionally, throw an exception if a transaction exists. I wanted to try like following: @Transactional(propagation = Propagation.NEVER) public void getDeps(long ID) { System.out.println(databaseImp.getDepartmentByID(ID)); } @Transactional(propagation = Propagation.REQUIRED) public void allProcessOnDB_second(long ID) { getDeps(ID); operation(ID); } @Transactional public void operation(long id){ System.out.println(databaseImp.getDepartmentByID(id)); }...

Spring 3.1.3 + Hibernate Configuration with annotations and with (Dynamic) AbstractRoutingDataSource

hibernate,spring-mvc,spring-security,spring-3,spring-annotations
I'm trying to change an annotation hibernate with only one datasource, to have as many as are saved in a database. In order to make an user have its assigned connection, and add new type of connections with only a server restart (avoiding .war recompile) The server first loads a...

PreAuthorize error handling

spring,spring-security,spring-boot,spring-annotations,spring-security-oauth2
I'm using Spring Oauth2 and Spring Pre-post Annotations With Spring-boot I Have a service class MyService. one of MyService methods is: @PreAuthorize("#id.equals(authentication.principal.id)") public SomeResponse getExampleResponse(String id){...} can i control in some manner the json that is returned by the caller Controller? the json that is returned by default is: {error...

Get DB column names from annotated bean in java

java,annotations,spring-annotations
In the below bean where the annotation is mapped to a DB column , how to get the values in a string. I need all values like below "TY_CD,ACTION_CD,ID" public class SampleBO implements SQLData{ @DbColumnMap(columnName = "TY_CD") private String typeCode; @DbColumnMap(columnName = "ACTION_CD") private String actionCd; @DbColumnMap(columnName = "ID") private...

When should we use @PreAuthorize and @Secured

java,spring,spring-security,annotations,spring-annotations
I read this stackoverflow post What's the difference between @Secured and @PreAuthorize in spring security 3? However,I am still not clear as to what is the big difference between the two in terms of security? In what scenarios should we go for @PreAuthorize as compared to @Secured?

Custom Spring annotation for request parameters

java,spring,spring-mvc,spring-annotations
I would like to write custom annotations, that would modify Spring request or path parameters according to annotations. For example instead of this code: @RequestMapping(method = RequestMethod.GET) public String test(@RequestParam("title") String text) { text = text.toUpperCase(); System.out.println(text); return "form"; } I could make annotation @UpperCase : @RequestMapping(method = RequestMethod.GET) public...

What is xml-configuration representation of @component in spring

java,spring,spring-annotations,xml-configuration
I have googled one of my problem and found the soulution via @component annotation. But in my application I'm using xml configuration, because of annotationes are nasty and not configurable, and you need to recompile all of the code co change smth. So, my question is: how do I use...

Spring - Autowiring a map

spring,spring-mvc,spring-annotations
I have a map in my spring xml defined as follows <!-- Home Screen Backrgound Image Service --> <bean id="homeScreenBackgroundImageService" class="com.services.images.impl.HomeScreenBackgroundImageServiceImpl" parent="imageServicesParent"> </bean> <util:map id="imageServicesMap" value-type="com.services.images.ImageServicesParent"> <!-- Map between String key and List --> <entry key="bean1" value-ref="homeScreenBackgroundImageService" /> When I inject this map into a class. My key is bean1...

Spring security using @security annotation in controller

java,xml,spring,maven,spring-annotations
I am working with spring security and create a demo application.It contains one login controller and 2 jsp pages for login and success. I want to aply method level security in controller using @secure annotation but is is not working, when I apply this in my service class it works...

Using Spring Security Annotations isFullyAuthenticated v/s hasRole

java,spring,spring-mvc,spring-security,spring-annotations
Being new to spring security annotations, I am not able to understand the real difference between the below two lines. Does it mean that hasRole implicitly checks for authentication? - @PreAuthorize("isFullyAuthenticated() and hasRole('activateUser')") + @PreAuthorize("hasRole('activateUser')") void activateUser(String username); As per spring documentation, isFullyAuthenticated() Returns true if the user is not...

Spring DI based on annotations

java,spring,servlets,dependency-injection,spring-annotations
i try to use spring, and stuck with spring configuration. As you see in title i try to configure on annotations and also try to use spring-boot (which is very good as i think). So my question is very simple (i think), is how to inject my bean to servlet...

Spring prototype bean reference in Spring @Configuration

java,xml,spring,spring-annotations
I went through the following page to bootstrap an applicationContext.xml in Java. http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch06.html My applicationContext has something like: <beans> <bean id="foo" class="com.mypackage.Foo" scope="prototype"> </bean> </beans> I need to refer to "foo" in Java as follows: @Configuration @AnnotationDrivenConfig @ImportXml("classpath:applicationContext.xml") public class Config { @Autowired Foo foo; @Bean(name="fooRepository") @Scope("prototype") public...

how to convert xml notation to annotation based notation : Spring - Java

java,spring,spring-annotations
I have following xml configuration in my spring context xml, I have used annotation based approach very less and unable to figure out how to represent following using annotation, need help. <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"...

Grails 2.4.4 spring security role doesn't apply to user

java,spring,grails,spring-security,spring-annotations
I have controller: class AdminController { def springSecurityService @Secured(['ROLE_ADMIN', 'ROLE_USER']) def index() { render "test"; } And user with role ROLE_ADMIN in the table: But, when I use: springSecurityService.getPrincipal().getAuthorities() There is only one role: ROLE_NO_ROLES Why? def loggedInUser = springSecurityService.currentUser; returns correct user. Config: ... grails.plugin.springsecurity.userLookup.userDomainClassName = '...' grails.plugin.springsecurity.userLookup.authorityJoinClassName =...

BeanCreationException from Spring with annotation “@Context” and “@Loggable”

java,spring,service,cxf,spring-annotations
I have web application using Spring, Apache CXF and Jetty. Spring doesn't like the addition of an annotation called @Loggable to methods in one of my classes. First, the annotation @Loggable links to the class LogAspect, which logs request and response messages: @Aspect @Component public class LogAspect { ... ......

Android as a Client, Server using Spring annotation,communication in JSON format,error while receiving in server

android,spring-annotations,jsonobject
I want to develop android app which is connected to server which is developed using Spring,and they should communicate in JSON format. But When I send request to server with JSON object and try to receive it,it is showing error java.lang.UnsupportedOperationException: JsonObject at com.google.gson.JsonElement.getAsString(JsonElement.java:191) at org.magnum.dataup.VendorController.addRestaurantWebView(VendorController.java:79) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at...

@Autowired finding ambiguous dependencies and still works. How?

java,spring,annotations,spring-annotations
Why is Spring not throwing NoSuchBeanDefinitionException where there are ambiguous dependencies and more than one bean candidates found for autowiring using @Autowired annotation ? I have this simple beans.xml that has two same beans with different ids category and category1 and for some reason Spring picks the category bean for...

Inject parameters to constructor through annotation in Spring

spring-boot,spring-annotations
I am using Spring Noot annotation configuration. I have a class whose constructor accepts 2 parameters (string, another class). Fruit.java public class Fruit { public Fruit(String FruitType, Apple apple) { this.FruitType = FruitType; this.apple = apple; } } Apple.java public class Apple { } I have a class that needs...

@PathVariable and @RequestParam not working together

spring,spring-mvc,annotations,spring-annotations,path-variables
I have below code in my controller: @RequestMapping(value = "/child/{nodeId}/{relType}",method = RequestMethod.GET, produces=MediaType.APPLICATION_JSON) public Iterable<Node> getConnectedNodes(@RequestParam("page")int page, @RequestParam("size")int size, @PathVariable("nodeId") String nodeId, @PathVariable("relType") String relType) { return nodeService.getConnectedNodes(nodeId, relType,page,size); } And this is the API url i am hitting...