My spring library : 3.2.0
Xml file :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<bean id = "res_Bean" class = "Restaurant"
<property name="welcomeNote" value="welcome to my restaurant"/>
</bean>
</beans>
Im trying to solve , but im not getting correct ans.
Exception in thread "main" java.lang.IllegalStateException:BeanFactory
not initialized or already closed - call 'refresh' before accessing
beans via the ApplicationContext at
org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:172)
at
org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1117)
at TestSpring.main(TestSpring.java:12)
When you define bean attribute class should be equals to full package path of the class.
For example
<bean id="restaurant" class="com.models.Restaurant">
<property <property name="welcomeNote" value="welcome to my restaurant"/>>
</bean>
Related
I have an active profile problem.
Decided to look in debug mode on my context
(profile indepenent contexts works in such way to in my case -
getBeanDefinitionNames returns empty array).
new FileSystemXmlApplicationContext with mentioned file works without exceptions in code fragment mode.
Code
new FileSystemXmlApplicationContext(
"D:\\Projects\\opti\dao\\src\\main\\resources\\contexts\\" +
"\\profiles\\test.xml")
This context with
getBeanDefinitionNames()
returns empty array.
D:\Projects\opti\dao\src\main\resources\contexts\profiles\test.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<beans profile="test">
<context:property-placeholder properties-ref="properties" ignore-resource-not-found="false"/>
<bean id="properties" lazy-init="false"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:properties/test-database.properties</value>
</list>
</property>
</bean>
</beans>
</beans>
Spring 4.2.5
D:\Projects\opti\dao\src\main\resources\properties\test-database.properties
connection.driverClassName=org.hsqldb.jdbc.JDBCDriver
connection.url=jdbc:hsqldb:mem:test
connection.userName=sa
connection.password=
For use beans of a specific profile you should start the jvm with -Dspring.profiles.active=test, or setting spring.profiles.active=test as env variable System.setProperties(props) could be a way for configure it.
I hope that this can help you
I am writing spring configurations for my project in JAVA. I am new to spring and not to able to figure out the errors related to spring.
I have the following file structure:
ProjectName
--> src
--> META-INF
--> jobs
-->edx
-->request-details.xml
-->clients.xml
-->daos.xml
-->environment.xml
--> request-details-edx-upload.xml
In META-INF/request-details-edx-upload.xml I import the following:
<import resource="jobs/environment.xml" />
<import resource="classpath:META-INF/jobs/edx/request-details.xml" />
<import resource="jobs/clients.xml" />
<import resource="jobs/daos.xml" />
But I get the following error:
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:META-INF/jobs/edx/request-details.xml]
Offending resource: class path resource [META-INF/request-details-edx-upload.xml]; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Cannot locate BeanDefinitionDecorator for attribute [dataSetName]
Offending resource: class path resource [META-INF/jobs/edx/request-details.xml]
request-details.xml is something like this:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
">
<!-- RequestDetails EDX data upload dao -->
<bean id="requestDetailsDataUploadDao" class="com.amazon.edx.dao.DataUploadDaoEdxClientImpl"
context:providerName="scot"
context:subjectName="${edxRequestDetailsSubject}"
context:dataSetName="by-01-day"
context:keyName="${regionValue}"
context:edxClient-ref="edxClient"
/>
<util:list id="requestDetailsColumnMetaData" value-type="com.amazon.edx.transformer.ColumnMetaData">
<ref bean="Column"/>
<ref bean="Column1"/>
</util:list>
<bean id="Column" class="com.amazon.edx.transformer.ColumnMetaData"
context:attributeName="Id"
context:dataType="VARCHAR2"
context:columnDisplayName="IMS"
/>
<bean id="Column1" class="com.amazon.edx.transformer.ColumnMetaData"
context:attributeName="id1"
context:dataType="VARCHAR2"
context:columnDisplayName="IMS"
/>
<util:constant id="tabDelimiter"
static-field="com.amazon.edx.transformer.Delimiters.TAB_DELIMITER" />
<!-- RequestDetails data backup -->
<bean id="requestDetailsDataTransformer" class="com.amazon.edx.transformer.DataTransformerImpl"
context:dataFlattener-ref="requestDetailsDataFlattener"
context:columnMetadata-ref="requestDetailsColumnMetaData"
context:delimiter="{tabDelimiter}"
/>
</beans>
Is there anything obvious I'm doing wrong with my import of the relative path?
Thanks.
Seems it is wrong schema definition. Try to remove namspace xmlns:context="http://www.springframework.org/schema/context" from <beans> tag. Instead add xmlns:p="http://www.springframework.org/schema/p" namespace. Also you should replace all context: suffix with p: suffix. Proper xml using shortcut with the p-namespace should looks like following:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- RequestDetails EDX data upload dao -->
<bean id="requestDetailsDataUploadDao" class="com.amazon.edx.dao.DataUploadDaoEdxClientImpl"
p:providerName="scot"
p:subjectName="${edxRequestDetailsSubject}"
p:dataSetName="by-01-day"
p:keyName="${regionValue}"
p:edxClient-ref="edxClient"
/>
....
</beans>
Hay ican't connect to database mysql
i'm using spring tool suite and phpMyAdmin with xampp on windows 8 64
this is my exception :
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.activation.DataSource] is defined: expected single bean but found 0:
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:271)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)
at pl.bnsmedia.tasks.Runner.main(Runner.java:32)
my configuration at app-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<description>Example configuration to get you started.</description>
<context:component-scan base-package="pl.bnsmedia.tasks" />
<context:property-placeholder location="classpath:META-INF/spring/service-config.properties"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:url="jdbc:mysql://localhost:3306/taskManager"
p:driverClassName="com.mysql.jdbc.Driver"
p:username="root"
p:password="sokol"
/>
i have mysql-connecor and spring-jdbc
i'm trying to do a simple code like :
ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/app-context.xml");
DataSource dataSource = context.getBean(DataSource.class);
System.out.println(dataSource);
an after i have a exception please help
Do it like this:
javax.sql.DataSource dataSource = context.getBean(javax.sql.DataSource.class);
You were using the wrong datasource: the exception says javax.activation.DataSource.
I'm trying to test a service with Mockito and testNG, but i have a couple of doubts. It's necessary create get/set to inject service, if service is declaredd like this:
#Autowired(required = true)
protected ITipService serveiTip;
when I'm trying to clean and package with maven I found this exception:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'consultaDeutes' defined in URL
[file:/D:/workspaceGPT/GPT/gpt.ui/target/test-classes/applicationContext-gui-deutes-Test.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'serveiTip' of bean class [cat.base.gpt.ui.ConsultaDeutesTest]: Bean property 'serveiTip' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
I believe that with autowiring get/set will be not necessary.
this is my test-context:
?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mockito="http://www.mockito.org/spring/mockito"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.mockito.org/spring/mockito https://bitbucket.org/kubek2k/springockito/raw/tip/springockito/src/main/resources/spring/mockito.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="cat.base.gpt.ui" />
<!-- mock del serveis que podem atacar per solicitar info -->
<mockito:mock id="serveiSubjecte" class="cat.base.tip.service.ISubjectesService"/>
<mockito:mock id="serveiTip" class="cat.base.tip.service.ITipService"/>
<mockito:mock id="serveiGpt" class="cat.base.gpt.domini.service.IGptService"/>
<mockito:mock id="sessio" class="cat.base.baseframe.session.IBaseSession"/>
<mockito:mock id="usuari" class="cat.base.baseframe.user.IBaseUser"/>
<!--
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:cat/base/bfp/ui/applicationResources" />
</bean>
-->
<bean name="consultaDeutes" class="cat.base.gpt.ui.ConsultaDeutesTest">
<property name="serveiTip" ref="serveiTip"/>
<property name="serveiGpt" ref="serveiGpt"/>
</bean>
</beans>
ApplicationContext:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mockito="http://www.mockito.org/spring/mockito"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.mockito.org/spring/mockito https://bitbucket.org/kubek2k/springockito/raw/tip/springockito/src/main/resources/spring/mockito.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="cat.base.gpt.ui" />
<!-- mock del serveis que podem atacar per solicitar info -->
<mockito:mock id="serveiSubjecte" class="cat.base.tip.service.ISubjectesService"/>
<mockito:mock id="serveiTip" class="cat.base.tip.service.ITipService"/>
<mockito:mock id="serveiGpt" class="cat.base.gpt.domini.service.IGptService"/>
<mockito:mock id="sessio" class="cat.base.baseframe.session.IBaseSession"/>
<mockito:mock id="usuari" class="cat.base.baseframe.user.IBaseUser"/>
<!--
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:cat/base/bfp/ui/applicationResources" />
</bean>
-->
<bean name="consultaDeutes" class="cat.base.gpt.ui.ConsultaDeutesTest"/>
<!-- WITH OUT PROPERTIES!!-->
</beans>
Using #Autowired will make spring automatically inject a matching bean into that field. Thus it is no longer required to define the "consultaDeutes" bean in the xml. If you'd like to use the xml definition, I believe you should define a setter for each property that you are trying to inject, eg: serveiTip, serveiGpt.
Using #Autowired in your test might require 2 additional annotation on the definition of your test class:
#ContextConfiguration(value = "/myContext.xml")
//#RunWith(SpringJUnit4ClassRunner.class) This is JUnit specific
#ActiveProfiles("dev")
public class TestCompareService {
#Autowired(required = true)
protected ITipService serveiTip;
....
}
I actually made a mistake pasting the #RunWith annotation specific for JUnit. For TestNG you can lookup this link. Apologies
Is there a special way for doing this?
What i got is:
config.properties with param.key=value
web.xml with ContextLoaderListener that reads the configuration
pages-servlet.xml that defines servlet beans.
What I want is to configure one of the beans in pages-servlet.xml with param.key.
I'm using <property name="myField" value="${param.key}"/> in the xml but I see that the field is configured with ${param.key} instead of 'value'.
What is the right way to configure the bean?
Ok, I solved it by importing application context file that defines configuration bean into pages-servlet.xml.
It works, but seems very wrong.
Property placeholder is what you want.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:property-placeholder location="classpath:/config.properties" />
<bean id="mybean" class="...">
<property name="xxx" value="${prop.value}" />
</bean>
</beans>