w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How to create a spring bean from an enum singleton's factory method
This ought to work: <util:constant id="exchangeFactory" static-field="com.xeiam.xchange.ExchangeFactory.INSTANCE" /> <bean id="mtGoxExchange" factory-bean="exchangeFactory" factory-method="createExchange"> <constructor-arg value="com.xeiam.xchange.mtgox.v2.MtGoxExchange" /> </bean> Give it a shot.

Categories : Java

Error creating bean with name 'flowBuilderServices': Initialization of bean failed with spring web flow
You need a reference to a view resolver. like so: <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean>

Categories : Maven

Spring Testing - Inject mocked bean with nested bean dependencies
You could use standalone mockMvc if you do want to unit test the controller. private MockMvc mockMvc; private SomeController controller = new SomeController(); @Mock private ResourceAdminService resourceAdminService; @Before public void setup() throws Exception { controller.setResourceAdminService(resourceAdminService); this.mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); } If you still want to setup an ApplicationContext, but it's too annoying with the DI. Maybe you'll interested in this article(http://java.dzone.com/articles/how-use-mockstub-spring). But the Strategy C:Dynamic Injecting will rebuild an ApplicationContext per test which could slow you tests if you have too many @Test methods.

Categories : Java

Concurrency control with method invocation in EJB bean vs. Spring bean
EJB declarative concurency management applies only to singleton session beans, see javax.ejb.ConcurrencyManagement API. Regular session beans have no thread safety issues because containter insures that only one thread accesses a bean instance at any given time. As for Spring beans concurrency is not managed by container, programmers themselves should take care of it.

Categories : Java

How can I access Spring bean from Message-driven bean in JBoss AS 7
A static holder for the context is not really a good idea. To make your beans available to other applications in a Java EE environment, you should consider making use of JNDI. Unfortunately, there is no plain JNDI exporter available out of the box, but it's fairly easy to write one yourself, as shown in this blog post: http://maestro-lab.blogspot.ro/2009/01/how-to-export-spring-managed-bean-to.html There is however a JndiRmiServiceExporter that you may want to look at. Once your beans are bound to names in JNDI, they can be referenced using standard CDI in your message bean without worrying about class loading issues.

Categories : Spring

When to declare a spring bean as private static when the bean is singleton?
If by declaring the bean as static, you mean something like @Bean public static MyBean myBean() { return new MyBean(); } then the benefit of this is to have your bean generated before the instance method beans. You would use this for example with a BeanFactoryPostProcessor which states: Application contexts can auto-detect BeanFactoryPostProcessor beans in their bean definitions and apply them before any other beans get created. The Spring Java configuration way for these to be auto-detected and initialized before the rest of your beans is to declare and initialize them with static methods. For example, @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } Spring will de

Categories : Spring

Spring bean scope as the class of the bean its autowired
U can use prototype scope. Then it will inject new instance of Executor when the Bean is constructed. So Executor starts its lifecycle when the bean is created. scoped-proxy=target-class creates CGLIB wrapper around your bean. So other beans holds reference only to this wrapper, and the right bean with right scope is dynamically injected into this wrapper. i.e. you don't hold very old expired request scoped bean in your session scoped bean, probably injected when the session was created . Instead new bean for current request is injected every time new request is created. This way you can access beans whith shorter life from beans with longer life i.e. your request scoped bean from your session scoped bean.

Categories : Spring

Can a bean be a value to another bean in the Spring Framework?
You can reference a bean like your dataSource. Your class should have a member that can hold the dataSource: package mypackage; public class MyBean { private DataSource dataSource; public void setDataSource(DataSource dataSource) { this.dataSource = data.Source; } } Then you can inject the dataSource bean into this bean: <beans> <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"> <!-- set properties --> </bean> <bean id="myBean" class="mypackage.MyBean"> <property name="dataSource" ref="dataSource"/> </bean> </beans> That's it.

Categories : Java

How to inject a JSF bean into a spring bean
If you are using Spring MVC, you can make your "bean" a Model Attribute, then have it auto-loaded into session using a combination of annotations on the Controller, handler methods, and a method that creates an instance of your bean. Your controller would look something like this: @Controller @SessionAttributes({"myBean"}) @RequestMapping("/myPath") public class MyController { @RequestMapping("/myPath2") public String myHandler(@ModelAttribute("myBean") MyBean myBean) { // ...do stuff, return view } @ModelAttribute("myBean") MyBean createMyBean() { // Create and init an instance return new MyBean(); } } You will get a MyBean magically created for you whenever there isn't one, and any time you update the Model (or ModelAndView) with "myBean", it will also get ma

Categories : Spring

ThreadPoolExecutor Future Task -exception with spring bean injection-spring-beans-2.0
The issue isn't with anything coming from java.util.concurrent or weblogic. What weblogic is telling you is that one of its registered threads has been waiting longer then 10 minutes for the get() to return. So why hasn't it returned? That is because the Callable you submitted has not yet returned. You should check the callable you submit to the executor and find out why that is still in the call() method. For instance, if I write something like Weblogic-Thread-1 Future f= e.submit(new Callable(){ public Object call(){ Thread.sleep(700000); return null; } }); Weblogic-Thread-2 f.get(); //will sit here and suspend for 700 seconds So you need to see why the task you submitted has not yet completed.

Categories : Java

Spring JSF integration: how to inject a Spring component/service in JSF managed bean?
@ManagedBean vs @Controller First of all, you should choose one framework to manage your beans. You should choose either JSF or Spring (or CDI) to manage your beans. Whilst the following works, it is fundamentally wrong: @ManagedBean // JSF-managed. @Controller // Spring-managed. public class BadBean {} You end up with two completely separate instances of the very same managed bean class, one managed by JSF and another one managed by Spring. It's not directly clear which one would actually be used in EL when you reference it as #{someBean}. If you have the SpringBeanFacesELResolver registered in faces-config.xml, then it would be the Spring-managed one, not the JSF-managed one. If you don't have that, then it would be the JSF-managed one. Also, when you declare a JSF managed bean spec

Categories : Spring

spring custome events:how to raise an event whenever a property values changed in spring bean
You can use Java's PropertyChangeLister to implement this. Please take a look at Vogel's tutorial on Observer Design Pattern in Java.

Categories : Java

Spring Bean not found for Spring Security RememberMe?
Cannot convert value of type [groovy.util.ConfigObject] to required type [int] for property 'tokenLength' This suggests to me that you don't have a grails.plugins.springsecurity.rememberMe.persistentToken.tokenLength property set in your grailsApplication.config - when you ask a ConfigObject for a non-existent key what it returns to you is a new empty ConfigObject.

Categories : Java

Confuse of Spring Xml config bean and Annotation config bean
You created two versions of one bean - one defined in applicationContext and one in dispatcherServlet. That usually wrong. As you suggest PlaceHolderConfigurer not shared beetween parent/child context. It works only for context where it defined.

Categories : Spring

Issue in understanding the Spring Bean Scopes Vs Spring web flow Session Scopes
You are confusing yourself with objects and beans. For each request, will it create a new Student Object or Spring container will create only once? The functioning of Spring is purely using beans. When you declare a something like a @Component, it's just an annotation that tells Spring that the part you've declared as a component is either Model or View or Controller i.e. a component of MVC. When you say something like @Scope=singleton, it tells Spring that only a single object instance can access the bean. Let me make it more clear. Say you and I are objects and a strawberry candy is a bean. So if you have the candy. I cannot take it from you. Meaning only one of us can have that candy. It's the same thing with singleton scope. Hope I made things simpler.. :)

Categories : Spring

Spring @Value does not inject property of one bean into another bean's property
You can inject bean A to B and then use @PostConstruct to set bP: @Component public class B { private boolean bP; @Autowired private A a; @PostConstruct public void postConstructMenthod() { bP = a.getAP(); } }

Categories : Spring

bean not injecting on spring MVC
You need to add the following to your servlet-context.xml configuration file, you don't need to declare annotated beans in the XML file. <mvc:annotation-driven /> <context:component-scan base-package="com.carloscortina" /> My suggestion would be to download Spring Stool Suite (STS) and create a new spring template project (selecting the MVC template), it will create a runnable project and that way you can see how everything is put together.

Categories : Spring

Spring No such bean when using * classpath
The dir/* expands to dir/a.jar dir/b.jar dir/c.jar, i.e. you are missing the colons : that separate entries in the classpath. Have a look at: Using bash, how do you make a classpath out of all files in a directory for a solution.

Categories : Java

spring--Cannot autowire bean
You need the @Autowired annotation on your type attribute, not the name one: public class Product { private int id; private String name; @Autowired private Type type; 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; } public void display(){ System.out.println("Id is: "+ id +" Name is: " + name +" Type is: " +type); } } It's not a bad idea to have setters/getters for Type as well, if only for consistency.

Categories : Spring

Spring Bean Scopes
From the spring specs, there are five types of bean scopes supported : 1. singleton(default*) Scopes a single bean definition to a single object instance per Spring IoC container. 2. prototype Scopes a single bean definition to any number of object instances. 3. request Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext. 4. session Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext. 5. global session Scopes a single bean definition to the

Categories : Java

Spring bean getting confused
The behavior is expected, this is because you have two beans with the exact same name info and the one defined later(in A) is the one which gets created at runtime. The behavior is same as what would have happened with the beans defined using xml. How this happens is best explained by understanding the internals of Spring @Configuration which is explained with the Spring documentation here - http://static.springsource.org/spring-framework/docs/3.2.3.RELEASE/spring-framework-reference/html/beans.html#beans-java-further-information-java-config. In short, even if you call info() method directly in AConfig, it is not actually invoking the method on the real AConfig instance, instead it is invoked on a proxy, which has the logic to make sure that the correct bean instance is returned.

Categories : Java

Bean dynamic values in Spring MVC
Instead of reading the values from a static Spring configuration file, read them at runtime from the database. When you want to change the values, you change them in the database, and the code reads the new values immediately without having to redeploy the application. You should only use configuration files for purely static values, that are chosen at deployment time and never change afterwards.

Categories : Spring

spring bean properties with singleton
It depends on what you say to Spring to do. Spring has an Inversion Of Control container that manages the instances of the objects. Depending on the scope you give to your objects, it will make them singletons or not. More info: http://static.springsource.org/spring/docs/3.0.0.M3/reference/html/ch04s04.html

Categories : Java

When to make Spring instantiate a bean?
Classes that provide services such as DAOs and Services (EmployeeDAO, EmployeeService) and its dependencies such as DataSources, etc. should be instantiated by Spring. Value objects such as Employee object itself should be instantiated by your code.

Categories : Spring

How to autowired bean of spring class
If you don't want a factory method, let Spring instantiate an instance of your class for you by annotating your class with @Component and make your @Configuration class @ComponentScan its package. When Spring scans that package, it will find your class, use its default constructor (or constructor annotated with @Inject or @Autowired) and use it to make an instance of your class. Spring will then add that instance to its context. Because PortResolveImpl is not under your control, you will need to use a @Bean factory method.

Categories : Java

Referring a component bean in XML - Spring
All annotated components will be available with the same class name (camelCase) by default. I think you can use that name in the XML file. <property name="taskList"> <list> <ref bean="componetClass1" /> <ref bean="componetClass2" /> <ref bean="componetClass3" /> <ref bean="componetClass4" /> </list> </property> In this case ComponentClass1 etc are the class names which are annotated with @Component annotation.

Categories : Java

When to use Spring to manage/instantiate a bean
The container is usually used as a top level entity responsible for instantiating all your objects. It solves the problem you eventually have to solve in that something has to new all your objects together and bring your application to life. Before dependency injection frameworks, people solved this problem by having separate Factory classes that were responsible for wiring together objects. In larger applications, this resulted in a lot of needless boilerplate classes that were doing nothing but calling "new" on a set of objects. Spring allowed you to essentially remove that concern from your application. For very small applications, you probably don't feel the burn of having to construct your application, and using a spring container is probably overkill. But for larger applications, u

Categories : Java

How to inject a Spring bean in an EJB3?
You may have already noticed but if in case you missed, beanRefContext.xml need to be passed your services-context.xml as argument, however it is missing in your code

Categories : Java

Accessing a Spring Bean from a Servlet
You can inject dependencies using annotations even in servlet (there is a special SpringBeanAutowiringSupport helper class for this pourpose): public class CustomServlet extends HttpServlet { @Autowired private ProductService productService; @Override public void init(ServletConfig config) throws ServletException { super.init(config); // inject productService dependency SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); } .... }

Categories : Spring

injection with spring in the managed bean :
First you will need a custom el-resolver in your faces-config.xml file: <application> <!-- For DI of beans from Spring WebApplicationContext --> <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> </application> Then you will need to use the annotation @ManagedProperty like this: @ManagedProperty(value = "#{clientService}") ClientService clientService; So the spring el-resolver can inject your service bean by name.

Categories : Java

Bean @Autowired by default in spring
try primary attribute, eg <bean id="b1" class="test.B" /> <bean id="b2" class="test.B" /> <bean id="b3" class="test.B" primary="true" /> this guarantees that b3 bean will be injected here public class Test { @Autowired B b; ...

Categories : Java

Spring error when add prarameter to bean
When you use @Configuration and @Bean annotations you configure beans, parameters for methods annotated with @Bean are threated as references to another beans, so you should add bean with class String and probably name of bean "userId" - name of parameter used for autowire by name.

Categories : Spring

Spring bean property define
As A has a public setter of field utility then you can directly set property of object in class A from B like. <bean name="b" class="com.dao.B"> <property name="utility" ref="utility"/> </bean> here utility is in class A with public setter and we are set value in B bean.

Categories : Java

Creating non-bean instances with Spring
You can make beans non-singletons if you like. Depends on whether you are ok with the XML: <bean id="beanA" class="misc.BeanClass" scope="prototype"> <property ... /> </bean> Which will give you a new instance every time. Each instance will be initialized with injected values. You can use this annotation too: @Scope("prototype")

Categories : Spring

inject spring bean in JSF component
UI components are not eligible for dependency injection. You've there a design problem. You shouldn't reference a managed bean (the controller) in UI component (the view) yourself. The enduser should do it by itself. E.g. <my:customComponent template="#{myBean}" /> Wrap if necessary in a tagfile to keep it DRY.

Categories : Spring

Passing spring constructor-arg to another ref bean
You can't do that with Spring; when you get myNewBean, myBeanFallback (myOldBean) property is correctly instatiated with null value as specified in constructor and you can't change this behaviour because myBeanFallback is not constructed using FactoryBean.getBean() but autowired. Maybe using a factory in this way can be a solution: class MyBeanFactory { public getNewBean(MyData mydata){ MyBean myBean = (MyBean) context.getBean("myNewBean", new Object[] { mydata }); MyBean myBeanFallback = getOldBean(myData); myBean.setMyBeanFallback(myBeanFallback); return myBean; } public getOldBean(MyData mydata){ return (MyBean) context.getBean("myOldBean", new Object[] { mydata }); } } and beans.xml <bean name="myNewBean" class="MyBean" scope="prototype" /> <

Categories : Java

Spring loading properties from a bean
Looking at the methods available (from JavaDoc for PropertyPlaceholderConfigurer) suggests to me no easy way to do such a thing. The general comment at the top suggests that, as of Spring 3.1, one should prefer to use PropertySourcesPlaceholderConfigurer instead. Using this solution, it appears that you could register your own set of PropertySource objects via the setPropertySources(...) method. Of course your Config class would have to be fit into the interface (abstract class actually) of PropertySource. It is worth noting that using the setPropertySources(...) method above will assume nothing else about your property sources and will therefore not add the default property sources (Environment and Local properties).

Categories : Spring

no such bean definition exception in spring
In your Book class you have a List<Author> but in your spring-model.xml file you refer to book1, book2 and book3 as instances of Book, not from Author probably due for copy/paste process and forgetting the most important step: adapt what you pasted. Just to show an example: <bean id="book1" class="com.vishal.Book"> It should be <bean id="book1" class="com.vishal.Author"> Also, you're referring to other beans using <ref bean="book1 " /> There's a white space in the end that should be trimmed. It should be <ref bean="book1" /> Similar for other bookX references in your spring-model.xml file.

Categories : Java

Choosing which bean to reference in spring?
The @Primary annotation may help you. If you have more than 1 implentation, the one annotated with @Primary is used

Categories : Java

Disable spring bean validation
Method validation in Spring is enabled via configuring a MethodValidationPostProcessor. In case you want to turn off method validation in certain scenarios you need to make sure no such processor is configured, e.g. by configuring it via a bean profile which is not active in this scenario. Method validation is not intended to be used only during development though, I'd recommend to have it enabled in production as well.

Categories : Spring



© Copyright 2017 w3hello.com Publishing Limited. All rights reserved.