For a pet project I would like to have an embedded Jetty run a Spring Web MVC app. I've used Spring in web containers (where it's easy to tell "where to start") and I've used embedded Jetty without Spring.
It feels a bit like the chicken or the egg problem if I want both to work together. What is the best way to organize the project? In other words, what shall I put in main()? Should it be a Spring app that happens to have Jetty as a bean (what about contexts then?)? Or should I start Jetty alone and plug Spring in via servlet listener? What are the caveats?
Jetty in a Spring container is used to start webapp, springified or not. The webapp and your webapp don't have the same Spring context without tricks.
So, you have to create a Jetty server in your main, add your webapp and start the server. The best way is using a web.xml like a common Java EE server, and add this descriptor to your Jetty server.
I think it is more reasonable to start Jetty alone and plug Spring in via servlet listener in web.xml. Let Spring manager all the app specific beans and let jetty focus on running your app, and maybe some day you can deploy you app to antoher servlet container without changing anything.
This is one way to embed Jetty in Spring
http://www.springbyexample.org/examples/embedded-spring-web-services.html
Related
I've gone through the Spring Boot docs but don't know if it is feasible to create a Spring Boot web application which invokes remote EJB 3.0 beans.
I don't have much experience with invoking EJBs from within Spring, but have read through Spring's Chapter 29 Enterise EJBs and it "seems" fairly straightforward.
However, is there anyway to create a Spring Boot app with an embedded container and still invoke remote EJBs? Or do I have to deploy a war to a JEE-enabled app server (ex: JBoss, Glassfish, Websphere, etc).
Are there any gotcha's or issues I have to consider when trying to invoke remote EJB 3.0 from within a Spring Boot app?
You do not need a container that provides a full Java EE implementation in order to access remote EJBs.
You can invoke a remote EJB from a simple command line program if you want.
Just follow the instructions that you linked for remote EJBs.
Note that configuration required for different target servers (where your remote EJB is hosted) can vary widely between implementations, so you will need to find out how to do it for that specific host.
You may find it easier to get a working configuration in a command line client before trying to get Spring set up because there will be less moving parts to deal with.
I am looking for a mvn project template that can be downloaded and used as a kick-off for a web application.
The requeriments are:
Spring security (for user authentication and pages accesing control)
Hibernate integration (for data persistance like users and more)
The application must run on tomcat (i use TomEE)
I have a web application already running with pages and servlets and daos, persistence.xml and more. The problem is that i cant find the way to integrate this app with spring security, and for this reason i am looking for a project template...
Give Spring Boot a shot. You can either use Spring Tool Suite or Spring Initializer site to get a secure web app running and using hibernate as ORM.
You can later choose to run the app in the embedded Tomcat/Jetty or package as WAR and deploy in container of your choice.
I have examples of both working individually, now trying to merge so that I have both capabilities / entry points.
Not surpised it is confused about the application contexts to load.
I want to know if this is even possible and if so any examples?
I know it is doable with spring boot however not wanting to move to that just yet.
I am attempting the impossible?
"Spring MVC (with embedded Jetty)" usually means an HTTP endpoint running in its own JVM (I suppose you have a main method that starts Jetty). Once you have that started, you can integrate it as any other HTTP endpoint in Spring Integration.
If you want them both to run in the same JVM, that probably means you don't want to make use of HTTP as you can invoke methods directly on your #Controllers or #Services.
Or perhaps I'm missing something here... ?
I'd go for Spring Boot. Version 1.1.6 was just released. I'm currently also migrating an "old" SI application to be based upon Spring Boot. I suggest you give http://start.spring.io a try. Migration should not be too difficult.
Here's the tutorial refering to the part where it quotes
The #ComponentScan annotation tells Spring to search recursively through the hello package and its children for classes marked directly or indirectly with Spring’s #Component annotation.
http://spring.io/guides/gs/rest-service/#_make_the_application_executable
I don't want to make my service an application, instead, I want to deploy it in JBoss as a WAR.
How do you make Spring find your Controller without the #ComponentScan
Lets start with the most strait forward question.
1) How do you make Spring find your Controller without the #ComponentScan
You can instantiate the Controller beans by explicite name them in the spring configuration xml
<bean id="myBean" class="com.example.MyBean">...</bean>
2) I don't want to make my service an application, instead, I want to deploy it in JBoss as a WAR.
A normal Spring Web Application is a normal WAR file that can been deployed like every other (Servlet based) war file in ever Servlet container, even to JBoss.
I think that you get confused by the term application, but I think that Spring, JBoss and you mean differentthings by an application
The Point is: that Component Scan is a way to find and instantiate Spring Beans (Java Objects that are maintained in a Spring Container), but this has no meaningful connection to "I don't want to make my service an application, instead, I want to deploy it in JBoss as a WAR."
I am using Spring to manage my DAO & Services. And JSF for UI. I want to use dependency injection in my JSF backing-bean. There is an article that explained how I can do that.
But I have two separate projects: one for Service and one for UI. The Spring configuration file is located in Service project.
How can I connect both project with Spring? I want to annotate my JSF pages for DI.
You can achieve this by using Spring Web Flow.
Spring have examples which show:
A JSF centric approach where your Spring and JSF beans are managed/configured the JSF way (faces-config) and a
Spring centric approach where your beans (including ManagedBeans) are managed in the Spring Context.
See Spring Flow Web Home
If you mean that you have one WAR with web services defined in it, and another separate WAR with the JSF stuff, I think it's really two separate projects each with their own Spring configuration.
The web service WAR will use either Spring web services or perhaps HTTP remoting to expose your service interfaces to clients via HTTP. This will have one set of application context configuration, either XML or annotations.
The JSF WAR will have the JSPs and controllers. The controllers will be injected with clients that will interact with the remote services to accomplish what you wish. That's all they need to know about the service WAR. There doesn't need to be any duplication of configuration at all.
It's actually a nice design, because it completely decouples the view from the rest of the problem.
Thank for everyone I did it. My mistake was with bean initialization. I tried to access my injected bean in constructor, but must must did in #PostConstruct method. And all that time i tried to find mistake in my config file. But it was in such simply place :)
I find some solution one:
Sample Application using JSF, Spring 2.5, and Java Persistence APIs with Glassfish v2
. But I have problem with it.
I can post this problem hear or must create new topic? Sorry for stupid question, i'm newbie her.