EntityManagerFactory not being injected using #PersistenceUnit - jpa-2.0

I'm a java beginner. I'm in trouble to configure a persistance unit using JTA transactions.
I need to use a PostgreSQL database that is already defined, configured and populated. Using netbeans, i created the persistance.xml and glassfish-resources.xml as fallows:
<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="WellWatcherPU" transaction-type="JTA">
<jta-data-source>WellWatcherDB</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.DefaultSessionLog"/>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
</persistence>
and
<resources>
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="org.postgresql.ds.PGSimpleDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="post-gre-sql_geowellex_geowellexPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
<property name="serverName" value="localhost"/>
<property name="portNumber" value="5432"/>
<property name="databaseName" value="DBNAME"/>
<property name="User" value="USER"/>
<property name="Password" value="PASSWORD"/>
<property name="URL" value="jdbc:postgresql://localhost:5432/DBNAME"/>
<property name="driverClass" value="org.postgresql.Driver"/>
</jdbc-connection-pool>
<jdbc-resource enabled="true" jndi-name="WellWatcherDB" object-type="user" pool-name="post-gre-sql_geowellex_geowellexPool"/>
</resources>
And this is how i get the EntityManagerFactory and EntityManager (as used in the netBeans example)
public class EUserDao {
#Resource
private UserTransaction utx = null;
#PersistenceUnit(unitName = "WellWatcherPU")
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager(); <-------- NullPointerException here
}
public EUser getOne(long userId){
EntityManager em = getEntityManager();
try {
return em.find(EUser.class, userId);
} finally {
em.close();
}
}
EDIT:
And here is my glassfish deploy log:
Informações: [EL Config]: 2012-05-10 12:01:13.534--ServerSession(2017352940)--Connection(1901223982)--Thread(Thread[admin-thread-pool-4848(5),5,grizzly-kernel])--connecting(DatabaseLogin(
platform=>DatabasePlatform
user name=> ""
connector=>JNDIConnector datasource name=>null
))
Informações: [EL Config]: 2012-05-10 12:01:13.534--ServerSession(2017352940)--Connection(1462281761)--Thread(Thread[admin-thread-pool-4848(5),5,grizzly-kernel])--Connected: jdbc:postgresql://localhost:5432/geowellex?loginTimeout=0&prepareThreshold=0
User: geowellex
Database: PostgreSQL Version: 9.1.3
Driver: PostgreSQL Native Driver Version: PostgreSQL 8.3 JDBC3 with SSL (build 603)
Informações: [EL Config]: 2012-05-10 12:01:13.534--ServerSession(2017352940)--Connection(766700859)--Thread(Thread[admin-thread-pool-4848(5),5,grizzly-kernel])--connecting(DatabaseLogin(
platform=>PostgreSQLPlatform
user name=> ""
connector=>JNDIConnector datasource name=>null
))
What's wrong?

Most likely problem is that your EUserDao is just regular class. Injection works only for container managed classes. Annotations like #PersistenceUnit and #Resource are not processed for normal classes.
Following types of classes are container managed classes (and in those #PersistenceUnit can be used):
Servlet: servlets, servlet filters, event listeners
JSP: tag handlers, tag library event listeners
JSF: scoped managed beans
JAX-WS: service endpoints, handlers
EJB: beans, interceptors
Managed Beans: managed beans
CDI: CDI-style managed beans, decorators
Java EE Platform: main class (static), login callback handler

I see that in your code declare:
private EntityManagerFactory emf = null;
but never create one... like this
emf = Persistence.createEntityManagerFactory("WellWatcherPU");
Thats why you get a Null Pointer Exception when use the object!
public EntityManager getEntityManager() {
return emf.createEntityManager(); <-------- NullPointerException here
}

Related

Unit Test for BPMN file

Hi we are using Activiti 6.0.0 along with Spring-Boot application on Spring Microservices. I have to write an Unit tests on bpmn files which have DelegateExpression. Can anyone point me the some ideas for UnitTest?
I've done a fully tested activiti project before. upon your question i uploaded it on my github activiti-test, so you can access complete and executable source of this answer. This project is based on Maven, Activiti, Spring, H2 and JUnit. but if you want to run it over Spring Boot so you can see my other sample project Spring-boot kickstart sample.
The activit-test project structure :
Maven Project POM.xml
You can import (activiti-test) project as existing Maven Project in your IDE or anywhere you are comfortable.
Important dependency is :
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
.
.
.
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>${activiti.version}</version>
</dependency>
.
.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
.
.
.
2. applicationContext.xml
The important code block is starting activiti engine (bean: processEngineConfiguration)
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<!-- H2 -->
<property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
<property name="driverClass" value="org.h2.Driver" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="databaseSchemaUpdate" value="true" /> <!-- important and valid values : true,false,create-drop -->
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="jobExecutorActivate" value="true" />
<property name="asyncExecutorEnabled" value="true" />
<property name="asyncExecutorActivate" value="true" />
</bean>
3. Create activiti database Schema
At the sample code there is a class bean named InitDb that call
org.activiti.engine.impl.db.DbSchemaCreate.main(null);
4. Prepare Test environment
There is a class named BaseTest to make other testing easy :
first part get instance of activiti service and the second part is a method to deploy a process by BaseTest child class that comes in the next code block.
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(BaseTest.APPCONTEXT)
public abstract class BaseTest implements ApplicationContextAware {
public static final String APPCONTEXT = "/applicationContext.xml";
Logger logger = Logger.getLogger(BaseTest.class);
public static ApplicationContext CONTEXT;
#Autowired(required=true)
protected ProcessDeployer processDeployer;
#Autowired
protected MyProcessEngine processEngine;
protected RuntimeService runtimeService;
protected TaskService taskService;
protected RepositoryService repositoryService;
protected HistoryService historyService;
#PostConstruct
public void init() {
runtimeService = processEngine.getProcessEngine().getRuntimeService();
taskService = processEngine.getProcessEngine().getTaskService();
repositoryService = processEngine.getProcessEngine().getRepositoryService();
historyService = processEngine.getProcessEngine().getHistoryService();
}
.
.
.
.
private List<String> deploymentIds = new ArrayList<String>();
#Before
public void deploy()
{
if(deploymentIds.size() > 0)
for(String deploymentId : deploymentIds)
repositoryService.deleteDeployment(deploymentId);
deploymentIds.clear();
long beforeCount = processDeployer.getDeployedCount();
logger.info("Number of Process Deployed Before start to deploy any new thing : " + beforeCount);
if(getBpmnFiles() != null)
for(String bpmn : getBpmnFiles())
{
if(bpmn != null)
deploymentIds.add(processDeployer.deploy(bpmn));
}
logger.info(processDeployer.getDeployedCount() + " Processes Deployed Successfully");
}
protected abstract String[] getBpmnFiles();
.
.
.
}
5. Simple Test Process Model
First of all we need a bpmn diagram you can find (SampleProcess.bpmn)
This process has a start event, one user task and an end event.
bpmn xml content is :
<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="Definitions_1" targetNamespace="http://org.eclipse.bpmn2/default/process">
<bpmn2:process id="process_1" name="sampleProcess">
<bpmn2:startEvent id="StartEvent_1">
<bpmn2:outgoing>SequenceFlow_2</bpmn2:outgoing>
</bpmn2:startEvent>
<bpmn2:endEvent id="EndEvent_1">
<bpmn2:incoming>SequenceFlow_3</bpmn2:incoming>
</bpmn2:endEvent>
<bpmn2:sequenceFlow id="SequenceFlow_2" sourceRef="StartEvent_1" targetRef="UserTask_1"/>
<bpmn2:userTask id="UserTask_1" name="User Task 1">
<bpmn2:incoming>SequenceFlow_2</bpmn2:incoming>
<bpmn2:outgoing>SequenceFlow_3</bpmn2:outgoing>
</bpmn2:userTask>
<bpmn2:sequenceFlow id="SequenceFlow_3" sourceRef="UserTask_1" targetRef="EndEvent_1"/>
</bpmn2:process>
</bpmn2:definitions>
6. Run a Test
After all you can run a test like this :
public class SampleProcessTest extends BaseTest {
#Override
protected String[] getBpmnFiles() {
return new String[] {"com/test/activiti/sampleProcess/SampleProcess.bpmn"};
}
#Test
public void sampleFlowTest()
{
ProcessInstance pi = super.runtimeService.startProcessInstanceByKey("process_1");
Task ut1 = super.taskService.createTaskQuery()
.processInstanceId(pi.getId())
.active()
.singleResult();
assertNotNull(ut1);
super.taskService.complete(ut1.getId());
HistoricProcessInstance historicProcess = super.historyService.createHistoricProcessInstanceQuery()
.processInstanceId(pi.getId())
.finished()
.singleResult();
assertNotNull("Process must be finished",historicProcess);
}
}
7.Delegate Expression example
At the source link there is lots of example of DelegateExpression, but for now we create a sample BPMN process has two service tasks that define by DelegateExpression to Autowire with Spring bean :
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
<process id="awProcess" name="My process" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<serviceTask id="servicetask1" name="Service Task" activiti:delegateExpression="${awServiceTask1}"></serviceTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
<serviceTask id="servicetask2" name="Service Task" activiti:delegateExpression="${com.test.activiti.autowiredservicetask.AWServiceTask2}"></serviceTask>
<sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="servicetask2"></sequenceFlow>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow3" sourceRef="servicetask2" targetRef="endevent1"></sequenceFlow>
</process>
</definitions>
and AWServiceTask1 :
#Component("awServiceTask1")
public class AWServiceTask1 implements JavaDelegate {
Logger logger = Logger.getLogger(AWServiceTask1.class);
#Override
public void execute(DelegateExecution execution) throws Exception {
logger.info("Object Identity : " + this.toString());
}
#PostConstruct
public void init()
{
logger.info("awServiceTask1 is created");
}
}
and similar way is the code of AWServiceTask2, you can find the fully source at activiti-test.
and test class is :
public class TestAWServiceTask extends BaseTest {
Logger logger = Logger.getLogger(TestAWServiceTask.class);
public static String KEY = "awProcess";
#Test
public void autoWireTest()
{
processDeployer.printAllProcessDefinition();
processEngine.getProcessEngine().getRuntimeService().startProcessInstanceByKey(KEY);
}
#Override
protected String[] getBpmnFiles() {
return new String[] {
"com/test/activiti/autowiredservicetask/AutoWireServiceTask.bpmn"};
}
}
I hope you got your desired answer

How to access a huge JSON coming (from a spring RESTful Service) in a spring MVC app using RestTemplate

My Spring RESTful web service is returning a JSON form of-
[{"key1":"value1","key2":"value2","key3":"value3"},{"key4":"value4","key5":"value5","key6":"value6"}]
Now when my spring MVC app, try to access it, to show in a JSP then Exception occurs saying-no suitable HttpMessageConverter found Please help me where I going wrong.Here is my code-
Inside #Controller class of my spring MVC app calling the RESTful service
//**com.songs.controllers.FrontSongController.java**
</*
author Rohit Tiwari
*/>
#RequestMapping(value="/alls",method=RequestMethod.POST)
public String getAllSongs(ModelMap md)
{
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>(headers);
String url="http://localhost:7001/SongAppWS/songappWS/allsongsWS";
RestTemplate rt=new RestTemplate();
//SongResource.class is for representation on incoming JSON see below for its code
//This is the line no 48 that you will see in below browser logs
ResponseEntity<SongResource> listofallsongs=rt.exchange(url,HttpMethod.GET,entity, SongResource.class);
md.addAttribute("listname", "Songs available in the repository:");
System.out.println("Response Entity object= "+listofallsongs);
System.out.println("Response Entity body= "+listofallsongs.getBody().toString());
return "Sucess";
}
Inside config-servlet.xml of my spring MVC app calling the RESTful service
<context:component-scan base-package="com.songs.controllers" />
<mvc:annotation-driven />
<context:annotation-config/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<bean class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</list>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Inside SongResource.java of my spring MVC app, which I am trying to use for converting the coming JSON to my SongResource.class object, that my spring MVC app can use in a jsp
//**com.songs.service.resource.SongResource.java**
public class SongResource
{
private String name;
private String film;
private String singer;
public SongResource(String name,String film,String singer)
{
this.name=name;
this.film=film;
this.singer=singer;
}
//setter & getters of all above
}
On calling the spring REST service from my spring MVC app the browser is saying as below-
Error 500--Internal Server Error
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [com.songs.service.resource.SongResource] and content type [application/json]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java :77)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:619)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:1)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:446)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:377)
at com.songs.controllers.FrontSongController.getAllSongs(FrontSongController.java:48)
//and so on
Try this, hope it will help you
#RequestMapping(value="/alls",method=RequestMethod.POST)
public String getAllSongs(ModelMap md)
{
String url="http://localhost:7001/SongAppWS/songappWS/allsongsWS";
RestTemplate rt=new RestTemplate();
SongResource[] songRs = template.getForObject(url, SongResource[].class);
List<SongResource> songs = Arrays.asList(songRs);
md.addAttribute("listname", "Songs available in the repository:");
md.addAttribute("listValues", songs);
return "Sucess";
}

Spring Test context scope

I am new in Spring test, and I have ran a lot of unit test successfully according to the documents step by step,however I have some questions:
1 Can all the TestCase use a global Spring Context
Now I configure each TeseCase the spring context like this:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(.....)
#Transactional
public class UserDaoTests {}
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(.....)
#Transactional
public class AccoutDaoTests {}
As shown, spring will load and destroy the same context again and again.
So I wonder if I can setup a global Spring context, and then make all the TestCases run inside this context?
2 Transaction management
It said that the Transaction Manager will rollback the operations to the database.
But I have not found in which case this feature will work.
Because in my application I use ORMLite instead of Spring JDBC.
And this is the configuration:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- our daos -->
<bean id="ormliteSource" class="com.j256.ormlite.jdbc.DataSourceConnectionSource" init-method="initialize">
<constructor-arg index="0" ref="dataSource"/>
<constructor-arg index="1" value="${jdbc.url}"/>
</bean>
<bean id="userDao" class="com.j256.ormlite.spring.DaoFactory" factory-method="createDao">
<constructor-arg index="0" ref="ormliteSource"/>
<constructor-arg index="1" value="com.springapp.model.User"/>
</bean>
And then in my test case:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(..)
#Transactional
public class UserDaoTests {
#Autowired
private Dao<User, Long> userDao;
#Test
public void testCreate() {
User u = new User();
u.setName("ysl");
u.setLocked(true);
try {
userDao.create(u);
} catch (SQLException e) {
log.error(e.getMessage());
e.printStackTrace();
}
}
}
Then I run the test, and I found that all the tests are successfully passed, but when I check the database, I found that there are some test data inserted, it seems that the rollback does not work.
Do I miss anything?
Spring should detect if it can setup a shared application context for you. Did you specify different xmls in different test cases? If your test case could run against a shared application context, you may try with:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(.....)
public abstract class AbstractSpringTests
public class ConcreteTests1 extends AbstractSpringTests
public class ConcreteTests2 extends AbstractSpringTests
In this case, spring should setup only one application context.
According to the reference, you should add #TransactionConfiguration instead of #Transactinal on test class(rollback is the default strategy if I'm not mistaken)
If you want a transaction to commit — unusual, but occasionally useful when you want a particular
test to populate or modify the database — the TestContext framework can be instructed to cause the
transaction to commit instead of roll back via the #TransactionConfiguration and #Rollback
annotations.

Spring Web Service Template: Add username token

I have a web app that acts as a client to a Jax-WS web service implemented using Spring WS. The Spring WS is configured to require a username token in the SOAP header. In the web app I plan to use the Spring web service template but I can't seem to find any examples which show how to add a UsernameToken to the outgoing request.
Can someone point me in the right direction?
Thanks.
You have to use Interceptors. See Chapter 7. Securing your Web services with Spring-WS.
The configuration would be something like this
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<property name="marshaller" ref="marshaller" />
<property name="unmarshaller" ref="marshaller" />
<property name="defaultUri"
value="http://localhost:8080/ws-demo/myws" />
<property name="interceptors">
<list>
<ref bean="wsSecurityInterceptor" />
</list>
</property>
</bean>
<bean id="wsSecurityInterceptor" class="org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor">
<property name="securementActions" value="UsernameToken"/>
<property name="securementUsername" value="Ernie"/>
<property name="securementPassword" value="Bert"/>
</bean>
in addition to jddsantaella's answer, the client class can use SAAJ to add username token in the SOAP header:
OrganisationPortfolioRequest request = WS_CLIENT_FACTORY.createOrganisationsPortfolioRequest();
OrganisationPortfolioResponse response;
response = (OrganisationPortfolioResponse) webServiceTemplate.marshalSendAndReceive(request,
new WebServiceMessageCallback() {
public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
SaajSoapMessage soapMessage = (SaajSoapMessage) message;
SoapEnvelope envelope = soapMessage.getEnvelope();
envelope.addNamespaceDeclaration("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
envelope.addNamespaceDeclaration("s", "http://company.com/ws/security.xsd");
SoapHeaderElement username = soapMessage.getSoapHeader().addHeaderElement(new QName("http://company.com/ws/security.xsd", "username", "s"));
username.setText(getCurrentUser.getUsername());
}
});
response.getResults();
Above given answer is used for xml.
I mention here annotation base configuration for usernameToken security policy for web service.
Add this configuration with spring boot client
#Bean
public WebServiceTemplate webServiceTemplate() {
WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
webServiceTemplate.setMarshaller(marshaller());
webServiceTemplate.setUnmarshaller(marshaller());
webServiceTemplate.setDefaultUri("http://localhost:8080/ws");
webServiceTemplate.setInterceptors(new ClientInterceptor[] {wsSecurityInterceptor()});
return webServiceTemplate;
}
#Bean
public Wss4jSecurityInterceptor wsSecurityInterceptor() {
Wss4jSecurityInterceptor wss4jSecurityInterceptor = new Wss4jSecurityInterceptor();
wss4jSecurityInterceptor.setSecurementActions(WSHandlerConstants.TIMESTAMP + " " + WSHandlerConstants.USERNAME_TOKEN);
wss4jSecurityInterceptor.setSecurementPasswordType(WSConstants.PW_TEXT);
wss4jSecurityInterceptor.setSecurementUsername("user");
wss4jSecurityInterceptor.setSecurementPassword("password");
return wss4jSecurityInterceptor;
}

property injection in apache cxf using spring

I have the following class
public class HeaderClass{
#Resource
private WebServiceContext webServiceContext;
public String getUserAgent() {
MessageContext msgCtx = webServiceContext.getMessageContext();
HttpServletRequest request = (HttpServletRequest)msgCtx.get(AbstractHTTPDestination.HTTP_REQUEST);
return request.getHeader("user-agent")
}
In my service bean class I want to inject this HeaderClass, so that I can use it there as follows:
package mypack;
#Path("/MyService")
public class MyServiceClass {
//May be some annotation has to be given here which I don't know
HeaderClass header;
public void useHeader() {
//Code to use the header
System.out.println(header.getUserAgent());
}
}
I have the following inside beans.xml file
<jaxrs:server id="SampleService" address="/">
<jaxrs:features>
<cxf:logging />
</jaxrs:features>
<jaxrs:serviceBeans>
<ref bean="MyServiceClass"/>
</jaxrs:serviceBeans>
</jaxrs:server>
<bean id="MyServiceClass" class="mypack.MyServiceClass"/>
I don't know how to add the property HeaderClass in the bean "MyServiceClass"
I am using apache cxf with spring configuration file (beans.xml).
Please help.
One way to achieve this is to add those lines to your beans.xml:
<bean id="HeaderClass" class="mypack.HeaderClass"/>
<bean id="MyServiceClass" class="mypack.MyServiceClass">
<property name="header" ref="HeaderClass" />
</bean>
You may also need to add a setHeader() method to your MyServiceClass.