I am trying to read properties from application.yml in spring-bean.xml like this:
<bean name="#{bean.name}" />
Is it possible ? or I should specify location of my application.yml file?
Yes It's Possible
For YAML Properties
You have to use YamlPropertiesFactoryBean
<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
<property name="resources" value="classpath:application.yml"/>
</bean>
<context:property-placeholder properties-ref="yamlProperties"/>
Then define your property in src/main/resource/application.yaml
bean:
name: foo
Now use can use the property in xml to create a bean
<bean name="${bean.name}"
class="net.asifhossain.springmvcxml.web.FooBar"/>
Here's my complete XML config
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" 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.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
<property name="resources" value="classpath:application.yaml"/>
</bean>
<context:property-placeholder properties-ref="yamlProperties"/>
<bean name="${bean.name}" class="net.asifhossain.springmvcxml.web.FooBar"/>
</beans>
Actually the question asked on SO continue. According to my spring-servlet.xml configuration for internationalization, I can not change the language of request. When I send request including "?language=tr" it returns me messages from en file. Any help would be appreciated.
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- SPRING INTERNALIZATION CONFIGURATION -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
p:paramName="language" />
</mvc:interceptors>
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>
<!-- Declare a view resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>
my applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />
<!-- spring frameworka properties dosyasını tanıtma -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/applicationContext.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
<aop:aspectj-autoproxy>
<aop:include name="myLogger"/>
</aop:aspectj-autoproxy>
<bean id="myLogger" class="sow.webservices.aop.SowLoggerAOP"/>
<bean id="personService" class="sow.webservices.services.PersonService" />
<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
For example #Controller and #Service. Make sure to set the correct base-package-->
<context:component-scan base-package="sow.webservices" />
<!-- Configures the annotation-driven Spring MVC Controller programming model.
Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
<mvc:annotation-driven />
<!-- Loads MongoDB configuraton -->
<import resource="mongo-config.xml"/>
</beans>
Rather make sure that you added the localeChangeInterceptor bean within the interceptors properties of your handleMapping bean as follows:
<bean id="handlerMapping"vclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
</bean>
Or try to register the LocaleChangeInterceptor as an mvc:interceptor:
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
p:paramName="language" />
</mvc:interceptors>
You need to add the mvc namespace schema (xmlns:mvc="http://www.springframework.org/schema/mvc") in the root declaration of the spring context file so it may look as:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
...
</beans>
I'm working on a Spring Roo web app, and need to use a Scheduled method that runs in background every certain time, then I added some
lines to my applicationContext.xml, and that method ran ok, but then
occured that the app wasn't able to set up/read the
transactionManager, the console says The matching wildcard is strict, but no declaration can be found for element 'tx:annotation-driven'.
I already searched, and found in this site, 2 or 3 answers to this error message, such aS:
add xmlns:tx="http://www.springframework.org/schema/tx to the
header [already done]
add component:scan before tx:annotation [already done] but when I try to run my app on the server, it throws
a HTTP Status 404 The Requested Resource is not available, could you
help me to fix the xml file?
this is my applicationContext.xml file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx">
<context:component-scan base-package="org.webapp.services.tasks">
</context:component-scan>
<task:annotation-driven />
<bean id="scheduledMethod" class="com.myhome.myproject.web.ScheduledJobsController">
</bean>
<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
<context:spring-configured/>
<context:component-scan base-package="com.myhome.myproject">
<context:exclude-filter expression=".*_Roo_.*" type="regex"/>
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="${database.driverClassName}"/>
...
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<context:component-scan base-package="com.myhome.myproject"></context:component-scan>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="persistenceUnitName" value="persistenceUnit"/>
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
You declared the usage of the tx namespace by doing
xmlns:tx="http://www.springframework.org/schema/tx"
But you forgot to declare the location of the xsd for this namespace in the xsi:schemaLocation attribute:
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
This is syntax which is working
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:application.properties"/>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
</bean>
This is alternative format which is NOT working
<context:property-placeholder location="classpath:application.properties" system-properties-mode="OVERRIDE"/>
Any ideas why? I will always crach on tests while building.
Application properties file contains just this
hibernate.show.sql = false
hibernate.format.sql = true
Your syntax looks correct. Do you have the context namespace registered in your context file?
For example:
<beans ...
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="...
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
I am trying to configure JSF+Spring+hibernate and I'm tying to run a test but when I use this "tx:annotation-driven" on my application-context.xml file, I get this error:
The matching wildcard is strict, but no declaration can be found for element 'tx:annotation-driven'
Here is my application-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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.6.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.6.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.6.xsd
" xmlns:tool="http://www.springframework.org/schema/tool">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:#192.168.56.101:1521:Gpsi"/>
<property name="username" value="omar"/>
<property name="password" value="omar"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>om.mycompany.model.Course</value>
<value>om.mycompany.model.Student</value>
<value>om.mycompany.model.Teacher</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction.manager="transactionManager"/>
<context:annotation-config/>
<context:component-scan base.package="com.mmycompany"/>
</beans>
and here is my CourseServiceImplTest. I have still not implemented the tests:
public class CourseServiceImplTest {
private static ClassPathXmlApplicationContext context;
private static CourseService courseService;
public CourseServiceImplTest() {
}
#BeforeClass
public static void setUpClass() throws Exception {
context=new ClassPathXmlApplicationContext("application-context.xml");
courseService=(CourseService) context.getBean("courseService");
}
#AfterClass
public static void tearDownClass() throws Exception {
context.close();
}
#Before
public void setUp() {
}
#After
public void tearDown() {
}
/**
* Test of getAllCourses method, of class CourseServiceImpl.
*/
#Test
public void testGetAllCourses() {
System.out.println("getAllCourses");
CourseServiceImpl instance = new CourseServiceImpl();
List expResult = null;
List result = instance.getAllCourses();
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
/**
* Test of getCourse method, of class CourseServiceImpl.
*/
#Test
public void testGetCourse() {
System.out.println("getCourse");
Integer id = null;
CourseServiceImpl instance = new CourseServiceImpl();
Course expResult = null;
Course result = instance.getCourse(id);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
and here is the CourseServiceImpl:
#Service("courseService")
#Transactional
public class CourseServiceImpl implements CourseService{
#Autowired
private SessionFactory sessionFactory;
#Override
public List<Course> getAllCourses() {
return sessionFactory.getCurrentSession().createQuery("from Course").list();
}
#Override
public Course getCourse(Integer id) {
return (Course) sessionFactory.getCurrentSession().get(Course.class, id);
}
#Override
public void save(Course course) {
sessionFactory.getCurrentSession().saveOrUpdate(course);
}
}
You have some errors in your appcontext.xml:
Use *-2.5.xsd
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
Typos in tx:annotation-driven and context:component-scan (. instead of -)
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="com.mmycompany" />
This is for others (like me :) ). Don't forget to add the spring tx jar/maven dependency.
Also correct configuration in appctx is:
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
, by mistake wrong configuration which others may have
xmlns:tx="http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
i.e., extra "/spring-tx-3.1.xsd"
xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
in other words what is there in xmlns(namespace) should have proper mapping in
schemaLocation (namespace vs schema).
namespace here is : http://www.springframework.org/schema/tx
schema Doc Of namespace is : http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
this schema of namespace later is mapped in jar to locate the path of actual xsd located in org.springframework.transaction.config
For me the thing that worked was the order in which the namespaces were defined in the xsi:schemaLocation tag : [ since the version was all good and also it was transaction-manager already ]
The error was with :
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
AND RESOLVED WITH :
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
I'm learning from udemy. I followed every step that my instructor show me to do.
In spring mvc crud section while setting up the devlopment environment i had the same error for:
<mvc:annotation-driven/> and <tx:annotation-driven transaction-manager="myTransactionManager" />
then i just replaced
http://www.springframework.org/schema/mvc/spring-mvc.xsd
with
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
and
http://www.springframework.org/schema/tx/spring-tx.xsd
with
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
actually i visited these two sites
http://www.springframework.org/schema/mvc/ and http://www.springframework.org/schema/tx/
and just added the latest version of spring-mvc and spring-tx i.e, spring-mvc-4.2.xsd and spring-tx-4.2.xsd
So, i suggest to try this.
Hope this helps.
Thank you.
One extra forward slash (/) in front of tx and the *.xml file troubled me for 8 hours!!
My mistake:
http://www.springframework.org/schema/tx/ http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
Correction:
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
Indeed one character less/more manages to keep programmers busy for hours!
In my case this was actually a symptom of the server, hosted on AWS, lacking an IP for the external network. It would attempt to download namespaces from springframework.org and fail to make a connection.
FWIW I had this same issue. Turned out my xsi:schemaLocation entries were incorrect, so I went to the official docs and pasted theirs into mine:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html section 16.5.6
I had to add a couple more but that was ok. Next up is to find out why this fixed the problem...
Make sure that Spring version and xsd version both are same.In my case I am using Spring 4.1.1 so my all xsd should be version *-4.1.xsd
Any one can help for me!!!!!!!!!
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop/ http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee/ http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang/ http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx/ http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd">
<context:annotation-config />(ERROR OCCUR)
<context:component-scan base-package="hiberrSpring" /> (ERROR OCCUR)
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages"></property>
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties"></bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${com.mysql.jdbc.Driver}"
p:url="${jdbc:mysql://localhost/}" p:username="${root}"
p:password="${rajini}"></bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${org.hibernate.dialect.MySQLDialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="employeeDAO" class="hiberrSpring.EmployeeDaoImpl"></bean>
<bean id="employeeManager" class="hiberrSpring.EmployeeManagerImpl"></bean>
<tx:annotation-driven /> (ERROR OCCUR)
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>