求《灵魂摆渡黄泉资源》百度云资源

【图片】spring mvc中在jsp中获取spring的bean【java吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:626,172贴子:
spring mvc中在jsp中获取spring的bean收藏
当然,除非是很奇葩的情况,一般不会这么干,但如果你真要这么干怎么办呢?我是搞游戏服务器的,测试妹纸每次要清号都找我,为了避免麻烦,于是想到用jsp写个页面,到正式环境,把jsp删除就可以了,但整个系统是基于spring的,所有对象都是spring创建的bean,在jsp难道基于jdbc 的原始redis去处理,于是到网上找教程,第一篇就是第一种肯定不行,于是我使用的是第二种方法,但是我这边获取到的ApplicationContext 对象为null,肯定不能通过他获取了,查看WebApplicationContextUtils源码可以看到,如果只传一个参数,会使用默认的attrName去ServletContext查找Attribute,于是我把所有的Attribute取出来,打印class,可以看到attrName为org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring对应的Attribute取出来的对象class为org.springframework.web.context.support.XmlWebApplicationContext那么直接通过WebApplicationContextUtils中的第二个方法,传入两个参数来获取ApplicationContext ,现在打印就不为空了拿到ApplicationContext,再要取什么bean,就随自己了。
青铜星玩家
百度移动游戏玩家均可认证(限百度账号),
半天没人顶一下,自己挽尊
青铜星玩家
百度移动游戏玩家均可认证(限百度账号),
登录百度帐号推荐应用spring bean的scope属性 - 为程序员服务
为程序员服务
spring bean的scope属性
spring中bean的scope属性,有如下5种类型:
singleton 表示在spring容器中的单例,通过spring容器获得该bean时总是返回唯一的实例
prototype表示每次获得bean都会生成一个新的对象
request表示在一次http请求内有效(只适用于web应用)
session表示在一个用户会话内有效(只适用于web应用)
globalSession表示在全局会话内有效(只适用于web应用)
在多数情况,我们只会使用singleton和prototype两种scope,如果在spring配置文件内未指定scope属性,默认为singleton。
下面我们用一个示例来说明singleton和prototype两种scope的区别。
新建一个maven项目添加spring相关的maven依赖,如果需要请参照:。
添加java类Person,Person的内容如下:
package cn.outofmemory.
public class Person {
public int getId() {
public void setId(int id) {
public String getName() {
public void setName(String name) {
this.name =
Person类是一个POJO类,我们不需要他做任何事情,只是为了证明prototype和singleton两种scope的异同之处。
我们还需要一个App类,他的代码如下:
package cn.outofmemory.
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
* Hello world!
public class App
public static void main( String[] args )
ApplicationContext appContext = new ClassPathXmlApplicationContext("/spring.xml");
Person p1 = appContext.getBean(Person.class);
System.out.println("p1's identityHashCode is " + System.identityHashCode(p1));
Person p2 = appContext.getBean(Person.class);
System.out.println("p2's identityHashCode is " + System.identityHashCode(p2));
在App类中的main方法中,首先我们初始化了ApplicationContext的实例,然后分别获得两次Person bean的实例p1,p2并分别输出其identityHashCode,如果两个对象是同一个对象,那么他们的identityHashCode应该是一致的。
添加source folder:src/main/conf,并新建spring配置文件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"
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"&
&bean class="cn.outofmemory.spring.Person"&
&property name="id" value="1"/&
&property name="name" value="Jim"/&
在此配置文件中我们定义了Person bean,没有指定其scope,这时候bean的scope为默认的singleton,也就是单例,此时App类的输出应该是两个相同的identityHashcode,我们运行程序看输出的结果:
p1's identityHashCode is
p2's identityHashCode is
可以看到p1和p2是完全相同的,我们再修改spring.xml配置文件,将Person bean的scope修改为prototype:
&bean class="cn.outofmemory.spring.Person" scope="prototype"&
&property name="id" value="1"/&
&property name="name" value="Jim"/&
再次运行程序,输出结果如下:
p1's identityHashCode is
p2's identityHashCode is
可以看到p1和p2是两个不同的值,这说明scope是prototype的情况下,同一个bean定义会返回不同的对象。
我们也可以通过Scope注解来指定java bean的scope,我们给Person类添加如下注解:
import org.springframework.context.annotation.S
import org.
@Component
@Scope("prototype")
public class Person {
@Component注解告诉spring,要加载此类,Scope注解bean的scope是prototype。
我们修改spring.xml配置文件,使用context:component-scan节点,让spring通过注解加载bean。
&?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"
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:component-scan base-package="cn.outofmemory.spring"&&/context:component-scan&
再次运行程序,将输出:
p1's identityHashCode is
p2's identityHashCode is
可以看到使用Scope注解指定prototype scope后,两个Person对象是两个不同的对象。
本文源码下载:
推荐阅读:
相关聚客文章匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。Spring MVC Controller配置方式 - java小强 - ITeye博客
博客分类:
Spring MVC 入门示例中,配置Controller时使用的是URL对应Bean的方式在SpringMVC中,对于Controller的配置方式有很多种,如下做简单总结
第一种 URL对应Bean如果要使用此类配置方式,需要在XML中做如下样式配置
&!-- 表示将请求的URL和Bean名字映射--&
&bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/&
&bean name="/hello.do" class="test.HelloController"&&/bean&
以上配置,访问/hello.do就会寻找ID为/hello.do的Bean,此类方式仅适用小型的应用系统
第二种 为URL分配Bean使用一个统一配置集合,对各个URL对应的Controller做关系映射
&!-- 最常用的映射配置方式 --&
&!-- &prop key="/hello*.do"&helloController&/prop&--&
&bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&
&property name="mappings"&
&prop key="/hello.do"&helloController&/prop&
&/property&
&bean name="helloController" class="test.HelloController"&&/bean&
此类配置还可以使用通配符,访问/hello.do时,Spring会把请求分配给helloController进行处理
第三种 URL匹配Bean如果定义的Controller名称规范,也可以使用如下配置
&!-- 将hello*.do交给helloController处理--&
&bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"&&/bean&
&bean name="helloController" class="test.HelloController"&&/bean&
第四种 注解首先在配置文件中开启注解
&!-- 启用 spring 注解 --&
&context:component-scan base-package="test" /&
&bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/&
在编写类上使用注解@org.springframework.stereotype.Controller标记这是个Controller对象使用@RequestMapping("/hello.do")指定方法对应处理的路径,这里只是简单示例,会有更复杂配置
代码类如下:
import java.util.D
import javax.servlet.http.HttpServletR
import javax.servlet.http.HttpServletR
import org.springframework.web.bind.annotation.RequestM
// http://localhost:8080/spring/hello.do?user=java
@org.springframework.stereotype.Controller
public class HelloController{
@SuppressWarnings("deprecation")
@RequestMapping("/hello.do")
public String hello(HttpServletRequest request,HttpServletResponse response){
request.setAttribute("user", request.getParameter("user") + "--&" + new Date().toLocaleString());
return "hello";
附件是源码,导入Eclipse查看示例。
请您到ITEYE网站看 java小强 原创,谢谢!
自建博客地址: ,内容与ITEYE同步!
下载次数: 285
浏览 36169
cuisuqiang
浏览: 2566702 次
来自: 北京
浏览量:2366788
页面还是jsp,怎么能叫做Freemarker入门示例呢?小强 ...
学习了! 用了这个方法,就不会阻塞了
用setField返回一些简单,重要的信息,不要保存太多的信息 ...
写道为什么我下载你的mypushlet.ra ...
为什么我下载你的mypushlet.rar 怎么出不来 报40 ...

我要回帖

更多关于 灵魂摆渡黄泉免费资源 的文章

 

随机推荐