spring的一些问题,高手解答
答案:2 悬赏:60 手机版
解决时间 2021-05-04 17:41
- 提问者网友:嘚啵嘚啵
- 2021-05-03 18:52
1、在不启动web服务器的情况下,如何获取spring容器中的bean对象?
2、spring是如何实现bean的?
3、spring的MVC开始时,控制器会返回一个包含视图数据的对象,请问是什么方法(写出方法参数和方法返回对象)
最佳答案
- 五星知识达人网友:孤独的牧羊人
- 2021-05-03 20:23
1.不启动web 情况下可以使用applicationContext 方法娶得bean,例子:以applicationContext.xml 路径是放在src文件夹中为例,采用junit作为测试类package org.lxh.test;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Test;import org.lxh.bean.Parameters;import org.lxh.service.interfaces.ParametersServiceIf;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ParametersTest {
private static ParametersServiceIf parametersServiceIf;
@BeforeClass public static void setUpBeforeClass() throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "applicationContext.xml"); parametersServiceIf = (ParametersServiceIf)applicationContext.getBean("parametersService"); }
@AfterClass public static void tearDownAfterClass() throws Exception { }
@Test public void testListParametersById() { try { Parameters parameters = parametersServiceIf.listParametersById(4); System.out.println(parameters.getCode()); } catch (Exception e) { e.printStackTrace(); fail("Not yet implemented"); } }}
2.spring是通过 IOC 依赖注入来实现查找bean的,根据xml配置文件或者根据注解名称进行查找.通过XMlBeanFactory进行解析xml然后在调用applicationcontext或者beanfactory进行实现调用bean,主要原理是使用反射创建bean的实例
3.springmvc 在2.5开始支持返回的对象有 void 类型,modelandview类型, String,modelMap类型,给你个我正在写教程的springmvc 注解版本的例子你参考一下
第一步配置 web.xml: <servlet> <description>This is Spring MVC DispatcherServlet</description> <servlet-name>SpringMVC DispatchServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <description>SpringContext</description> <param-name>contextConfigLocation</param-name> <param-value>classpath*:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC DispatchServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
配置 applicationContext.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:aop=" http://www.springframework.org/schema/aop" xmlns:context=" http://www.springframework.org/schema/context" xmlns:mvc=" http://www.springframework.org/schema/mvc" xmlns:tx=" http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 对spring org.lxh包下所有注解扫描 --> <context:component-scan base-package="org.lxh"></context:component-scan>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀,在requestmapping输入的地址后自动调用该类进行视图解析--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix" value="/" /> <property name="suffix" value=".jsp"></property> </bean>
</beans>
Java类:package org.lxh.mvc.controller;
import java.util.HashMap;import java.util.Map;
import org.lxh.bean.UserBean;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;
@Controllerpublic class RequestMappingStudy1 { private Logger logger = LoggerFactory.getLogger(RequestMappingStudy1.class);
@RequestMapping("/novoid") public void novoid() { logger.info("test1 method is using"); }
@RequestMapping("/string") public String string() { return "WEB-INF/jsp/success"; }
@RequestMapping("/modelview") public ModelAndView view(Model model) { ModelAndView andView = new ModelAndView(); andView.setViewName("WEB-INF/jsp/success"); return andView; }
@RequestMapping("/map") public ModelMap map(){ ModelMap map =new ModelMap(); map.addAttribute("aa", "bb"); map.addAllAttributes(temp()); return map; }
public Map<String,Object> temp(){ Map<String, Object> map1 = new HashMap<String, Object>(); UserBean bean =new UserBean(); bean.setId(1); bean.setUsername("Edward Lau"); bean.setPassword("edward"); map1.put("hello", "world key"); map1.put("user", bean); return map1; } @RequestMapping("/model") public void model(Model model){ Xml } }
全部回答
- 1楼网友:woshuo
- 2021-05-03 20:29
spring的核心是依赖注入,必须装载配置文件,根据配置文件依赖注入,你可以写个main方法装载
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯