I have use a .dtd to my applicationContext.xml, but now i want to use Spring's AOP based on annotation. I've been told to add a in my applicationContext.xml.
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<aop:aspectj-autoproxy />
...
But something wrong happens. It seems that the file doesn't recognize the aop node, so i think i should import one more .dtd file, and i find this:
<!DOCTYPE aspectj PUBLIC
"-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
but can i use both .dtd togeter? how?
thx
You don't have to use DOCTYPE here, better declare xml namespaces like this:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<beans>
<aop:aspectj-autoproxy />
...
xmlns="http://www.springframework.org/schema/beans" means that beans will be root namespace (you don't have to use <beans:bean>) and aop will be accesible as desired.
The two DTDs you cite are not constructed in a way that allows them to be used together. In particular, the definition of beans in http://www.springframework.org/dtd/spring-beans.dtd is just
<!ELEMENT beans (
description?,
(import | alias | bean)*
)>
It does not provide for a child named aop:aspectj-autoproxy, and it doesn't provide any mechanism for later users like you to add new things to the content of beans.
DTDs can be built for extensibility and to support the integration of elements from multiple namespaces, though it requires a bit of forethought and planning. When extension points are not included, it's typically pretty hard or impossible to extend the DTD without just rewriting it.
Related
To work via annotations in Spring need to define:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:context="http://www.springframework.org/schema/context">
<context:annotation-config/>
</beans>
at
annotation-config.xml
But I created a simplest Spring boot Application (lets say I select lust Web in initialazr)
It works on annotations but there isn't any annotation-config.xml there and nor mentioning of ,
where is hidden?
You only need to add <context:annotation-config /> or <context:component-scan /> (which implies annotation driven config) when using an ApplicationContext implementation that doesn't support annotations out-of-the-box.
When only using XML based configuration you also use one of the XML enabled ApplicationContext implementations, generally that would be XmlWebApplicationContext. With these you would need to instruct the ApplicationContext to enable annotation processing.
When using Java based configuration you generally use an annotation based ApplicationContext, default would be AnnotationConfigWebApplicationContext. Due to its nature of processing Java configuration classes it has annotation processing enabled by default.
Spring Boot uses the latter (it actually uses a specialized subclass for this). Hence you don't need to explicitly enable it.
In Spring config xml we have tag and in that tag we have xmlns, xsi etc..
what all these details means ? When and how these details matters?
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.5.xsd">
</beans>
thanks,
You can find the nice answer of xsi:schema use here Use of xsi:schemaLocation
And this spring document will help you understand the spring configuration in details XML Schema-based configuration
In Spring there is a XML configuration for bean right?
What is the difference between:
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
and
<beans xmlns="http://www.springframework.org/schema/beans"
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.0.xsd">
Also i'm a bit curious with the difference between SpringMVC, MVC and just Spring
Difference is in formats of documents definitions. First is called DTD, second - XSD. Both are used to describe possible contents of xml document. DTD is older than XSD. XSD is more flexible and powerful than DTD. More differences you can see here.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- bean definitions here -->
</beans>
The equivalent file in the XML Schema-style would be…
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<!-- bean definitions here -->
</beans>
The above Spring XML configuration fragment is boilerplate that you can copy and paste (!) and then plug definitions into like you have always done.
Differences between an XML Schema Definition (XSD) and Document Type Definition (DTD) include: XML schemas are written in XML while DTD are derived from SGML syntax. XML schemas define datatypes for elements and attributes while DTD doesn't support datatypes. ... XML schemas are extensible while DTD is not extensible.
I'm making my first forays into spring and am getting the following error trying to run on eclipse:
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'model' must be of type [com.myCompany.project.Model], but was actually of type [com.myCompany.project.Model]
Code causing the exception:
import com.myCompany.project.Model;
// some code
public Model getModel() {
ClassPathXmlApplicationContext applicationContext =
new ClassPathXmlApplicationContext("client-context.xml");
return applicationContext.getBean("model", Model.class);
}
Spring 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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<bean id="model" class="com.myCompany.project.Model" />
</beans>
The client-context.xml is located in project/resources, the code in project/src/main/
I assume this is a classpath issue, but I am at a loss as to what to do to fix it.
Java and Spring are case sensitive. This:
<bean id="model" class="com.myCompany.project.model" />
is not the same as this:
<bean id="model" class="com.myCompany.project.Model" />
I'd think about some better names. Those aren't very insightful.
I don't like the way you're going about this. You shouldn't have a bean that has to access the app context this way. You'll have to post more code to be sure, but you ought to be wiring that model bean into object that wants it, not doing what you're doing.
The only reason for doing what you're proposing is if the bean interacting with the app context is creating using new rather than the Spring bean factory. Since you're just starting with Spring, I would recommend letting Spring handle all dependencies.
I'am trying to configure spring with XML syntax only and come into a problem with native resource exceptions translation to DataAccessException. According to documentation I always need to put #Repository on repository bean and declare PersistenceExceptionTranslationPostProcessor bean. I think that here should be the way to define some sort of filter using AOP which will perform exceptions translation but can't find anything like this in docs. The idea is to introduce convention, for example 'for everything what ends with Dao apply native exceptions translation'. Any ideas?
Similar to #Repository annotaion spring xml config looks like:
<?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"
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">
...
<bean id="translationInterceptor" class="org.springframework.dao.support.PersistenceExceptionTranslationInterceptor" />
<aop:config>
<aop:advisor advice-ref="translationInterceptor" pointcut="within(org.example.spring.dao..*Dao)" />
</aop:config>
...
</beans>
Pointcut "within(org.example.spring.dao..*Dao)" applies interceptor to methods in classes located in package org.example.spring.dao and its subpackages ends with "Dao" suffix