I'm trying to inject a Map into a Grails controller. I want to inject the same map into many controllers so would like to define it in resources.groovy.
I've looked on the web but can't find an example of creating a simple map.
In Spring MVC I've used something like this:
<util:map id="diplomaPermissions">
<entry>
<key>1</key>
<value>Diploma_AQA_Building_And_Construction</value>
</entry>
<entry>
<key>1</key>
<value>Diploma_Edexcel_Building_And_Construction</value>
</entry>
</util:map>
With this in my xml header:
<?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"
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/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
But this doesn't seem to work in grails if I use the spring xml files.
Any tips appreciated.
after further investigation, you can create the map in "resources.groovy"
beans = {
diplomaPermissions(org.springframework.beans.factory.config.MapFactoryBean) {
sourceMap = [
1:"Diploma_AQA_Building_And_Construction",
2:"Diploma_Edexcel_Building_And_Construction"
]
}
}
I think it might be related to the version of spring you are expecting; when using the old style for creating map, everything works fine.
try this in your resources.xml in the spring configuration directory
<bean id="testMap"
class="org.springframework.beans.factory.config.MapFactoryBean">
<property name="sourceMap">
<map>
<entry key="key1" value="value1"/>
<entry key="key2" value="value2"/>
</map>
</property>
</bean>
and this on your controller
class DisplayMapController {
def testMap
def index = {
render (contentType: "text/plain") {
div(id:"myDiv") { p "$testMap" }
}
}
}
Related
I am new to grails and i want to convert xml configuration to resource.groovy. but there are namespaces in xml. i don't want to duplicate config here.there should be one configuration which would be resource.groovy
my xml is
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
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-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/neo4j
http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd">
<context:component-scan base-package="neo4j"></context:component-scan>
<util:map id="config">
<entry key="ha.server_id" value="2" />
<entry key="ha.initial_hosts" value="127.0.0.1:5001,127.0.0.1:5002" />
<!-- put in more config parameters here, http://docs.neo4j.org/chunked/stable/ha-configuration.html -->
</util:map>
<bean id="graphDbFactory"
class="org.neo4j.graphdb.factory.HighlyAvailableGraphDatabaseFactory" />
<bean id="graphDbBuilder" factory-bean="graphDbFactory"
factory-method="newHighlyAvailableDatabaseBuilder">
<constructor-arg value="/home/alok/Desktop/data4" />
</bean>
<bean id="graphDbBuilderFinal" factory-bean="graphDbBuilder"
factory-method="setConfig">
<constructor-arg ref="config" />
</bean>
<bean id="graphDatabaseService" factory-bean="graphDbBuilderFinal"
factory-method="newGraphDatabase" destroy-method="shutdown" />
<neo4j:config graphDatabaseService="graphDatabaseService" base-package="neo4j"/>
<neo4j:repositories base-package="neo4j" />
</beans>
I want to know how there i have to handle namespaces and constructor-arguments
You really have two questions here, but let's answer both of them.
How do you use name spaces in resources.groovy?
Here is how:
// resources.groovy
beans {
xmlns neo4j:"http://www.springframework.org/schema/data/neo4j"
xmlns context:"http://www.springframework.org/schema/context"
... // and so on.
// neo4j.repositories
}
How do you use constructor-arguments in resources.groovy?
Here is how:
// resources.groovy
beans {
someBean(
SomeBeanClass,
'stringArg1',
['listOfStringsArg2', 'listOfStringArg2-a'],
ref('someOtherBeanAsArg3')
)
}
I have an xml type grails.converters.deep.XML
<?xml version="1.0" encoding="UTF-8"?>
<list>
<customer>
<name>A</name>
<age>1</age>
</customer>
<customer>
<name>B</name>
<age>2</age>
</customer>
<customer>
<name>C</name>
<age>3</age>
</customer>
</list>
How do I get each customers and how do I convert it to customer domain object?
Thanks,
Nimmy
I think the deep converters have been deprecated. The new way is supposed to be something like:
XML.use( 'deep' ){ ... }
http://johnrellis.blogspot.co.uk/2009/11/grails-and-json-are-pretty-groovy.html
But you should really be using the new render as json functionality in grails 2
http://grails.org/doc/latest/guide/theWebLayer.html#xmlAndJSON
I'm trying to inject a PersistenceContext using EJB3 annotations but geronimo doesn't inject dependency. It's an EAR project composed by an EJB and a WEB module.
EJB-module configuration
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ejb:openejb-jar
xmlns:app="http://geronimo.apache.org/xml/ns/j2ee/application-2.0"
xmlns:bp="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:client="http://geronimo.apache.org/xml/ns/j2ee/application-client-2.0"
xmlns:conn="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2"
xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2" xmlns:ejb="http://openejb.apache.org/xml/ns/openejb-jar-2.2"
xmlns:jaspi="http://geronimo.apache.org/xml/ns/geronimo-jaspi"
xmlns:log="http://geronimo.apache.org/xml/ns/loginconfig-2.0"
xmlns:name="http://geronimo.apache.org/xml/ns/naming-1.2" xmlns:pers="http://java.sun.com/xml/ns/persistence"
xmlns:pkgen="http://openejb.apache.org/xml/ns/pkgen-2.1" xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0"
xmlns:web="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1">
<dep:environment>
<dep:moduleId>
<dep:groupId>wedge</dep:groupId>
<dep:artifactId>wedge-ejb</dep:artifactId>
<dep:version>1.0</dep:version>
<dep:type>car</dep:type>
</dep:moduleId>
<dep:dependencies>
<dep:dependency>
<dep:groupId>console.dbpool</dep:groupId>
<dep:artifactId>jdbc_wedgeDS</dep:artifactId>
<dep:version>1.0</dep:version>
<dep:type>car</dep:type>
</dep:dependency>
</dep:dependencies>
</dep:environment>
I've configured persistence.xml as follows
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="wedgePU" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>jdbc/wedgeDS</jta-data-source>
<class>wedge.entity.Aec</class>
...
<class>wedge.entity.Tnr</class>
<properties>
<property name="openjpa.TransactionMode" value="managed" />
<property name="openjpa.ManagedRuntime"
value="jndi(TransactionManagerName=java:comp/TransactionManager)" />
<property name="openjpa.Log" value="DefaultLevel=INFO" />
</properties>
</persistence-unit>
I've created datasource as Local TX Datasource, and is resolved ok.
I've defined an EJB as follows
#Local(PruebaBL.class)
#Stateless
public class PruebaBLImpl {
#PersistenceContext()
private EntityManager em;
#TransactionAttribute(TransactionAttributeType.REQUIRED)
public void metodoPrueba(HttpServletResponse response) throws IOException, NamingException {
if (em == null) {
response.getOutputStream().println("entity manager is null");
}
I've verified that transaction begins/ends OK, but entityManager is not injected.
Some ideas?
Thanks in advance.
Saul
You don't need to specify the global jndi name. Inject it like this in your servlet:
#EJB
private PruebaBL pruebaBL;
I am trying to configure Spring Security in my simple application. Here is my configuration file, security.xml:
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:b="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<http>
<form-login login-page="/login/" authentication-failure-url="/fail/" />
<logout logout-success-url="/" />
</http>
<authentication-manager>
<authentication-provider user-service-ref='myUserDetailsService' />
</authentication-manager>
<b:bean id="myUserDetailsService" class="my.package.security.MyUserDataService" />
</beans:beans>
I got following errors with deploying:
The prefix "beans" for element "beans:beans" is not bound.
How can I fix this problem?
You're missing up the beans and b prefixes. You've declared the b prefix, and then used the beans one. You need to pick one and stick with it. For example, replace
xmlns:b="http://www.springframework.org/schema/beans"
with
xmlns:beans="http://www.springframework.org/schema/beans"
and then
<b:bean...
with
<beans:bean...
You're declaring the namespace as b: and using it everywhere except the enclosing beans tag, in which you're using beans:beans instead of b:beans.
I was playing with Weld-SE (Java SE) and noticed that if there are a lot of JARs in the classpath, the JVM startup time runs into several seconds.
Isn't there a way to specify/restrict the scan path as a package pattern or path pattern like in Apache Ant or AspectJ?
PS: Registration on Weld forum just does not work - it keeps saying "your password is trivial"
Starting with weld 1.1.0, it is possible according to Weld reference documentation :
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:weld="http://jboss.org/schema/weld/beans"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee http://docs.jboss.org/cdi/beans_1_0.xsd
http://jboss.org/schema/weld/beans http://jboss.org/schema/weld/beans_1_1.xsd">
<weld:scan>
<weld:exclude name="mypackage.MyClass"/>
</weld:scan>
</beans>
You can with CDI 1.1. The 1st answer works fine, but this snippet works on any provider:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="all">
<scan>
<exclude name="my.cool.package" />
<!-- you can exclude with condition -->
<exclude name="my.cool.package.for.jodatime" />
<if-class-not-available name="org.joda.time.LocalDate"/>
</exclude>
</scan>
</beans>
Good questions, but I don't think it is possible. Each archive is scanned for beans.xml, by spec.