我不确定如何在这里说出标题,因此我也不确定如何去寻找答案.
我有一个处理请求的java servlet引擎.假设我们有一个doGet请求:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//set up user data
//do whatever the user requested
SomeClass c = new SomeClass();
c.doSomething();
}
Run Code Online (Sandbox Code Playgroud)
现在在doSomething中,我希望能够访问哪个用户发出了请求.现在我通过创建一个java对象并将其传递到我需要的任何地方来做到这一点:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//set up user data
MyUserObj userObj = new MyUserObj();
userObj.setId('123');
//do whatever the user requested
SomeClass c = new SomeClass(userObj);
c.doSomething();
}
Run Code Online (Sandbox Code Playgroud)
通过这样做,我可以访问MyUserObj的实例,并且可以根据需要进一步传递.
这感觉很草率.在asp.net MVC3中(我目前正在学习它,在我开始学习java之后开始,但发现这非常方便,所以让我重新思考我是如何在java中做的)我可以,例如,存储当前的项目像这样的线程: doGet(). doSomething()然后可以在其他函数中使用,而不必传递一个对象.
有没有一种方法在java中为每个请求设置一些变量(甚至设置MyUserObj以后访问)而不将对象作为参数传递?
如果这是一个常见的已经回答的问题,请随意指出我正确的方向,因为我不知道该怎么说.
我正在研究分页,我有一些问题。
我认为基于光标的要复杂得多,这使得基于偏移量的分页更加可取。只有以实时数据为中心的系统才需要基于游标的分页。
我使用Spring @Configuration来配置我的应用程序.
目前我有一个@Configuration类,其中声明了我的所有bean.随着bean数量的增长(超过30个),我想将它分成许多类.
一些bean使用公共类(主要是实用程序类):
Foo.class是一个实用类 Bar.class和Baz.class都使用Foo.class
我希望在三个不同的@Configuration类(分别为Conf1,Conf2和Conf3)中使用Foo,Bar和Baz
问题是我无法访问Conf2和Conf3中的Conf1实例:
Conf1.class
@Configuration
public class Conf1 {
@Bean
public Foo foo() {
return new Foo();
}
}
Run Code Online (Sandbox Code Playgroud)
Conf2.class
@Configuration
public class Conf2 {
@Bean
public Bar bar() {
Bar bar = new Bar();
bar.setFoo(conf1.foo()); // Not possible !
return bar;
}
}
Run Code Online (Sandbox Code Playgroud)
Conf3.class
@Configuration
public class Conf3 {
@Bean
public Baz baz() {
Baz baz = new Baz();
baz.setFoo(conf1.foo()); // Not possible !
return baz;
}
}
Run Code Online (Sandbox Code Playgroud)
关于如何解决这个问题的任何想法?
用C编写的多线程进程几乎耗尽了所有的系统内存。为了找出消耗大部分内存的线程,我创建了一个核心文件gcore [pid]来检查每个线程的内存使用情况,但我找不到方法来做到这一点。
ps -eLFlm带有-H选项的 top 命令显示总内存消耗,但不是每个线程。
有什么有用的提示可以解决问题吗?
操作系统:Centos6
这是我的第一篇文章,所以请随时留下一些反馈,或者如果我做错了什么:)
我正在使用 spring-boot 和 resteasy :
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.paypal.springboot</groupId>
<artifactId>resteasy-spring-boot-starter</artifactId>
<version>2.3.4-RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 Swagger 来查看我的端点,所以我添加了这个依赖项:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
此依赖项已加载到我的存储库中,一切看起来都不错。
我添加了这个类来制作最简单的配置类:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
当我在调试模式中启动应用程序时,我输入了这个以前的 Bean。一切看起来都很好。
当我启动 Spring 应用程序时:
@SpringBootApplication
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class,RabbitAutoConfiguration.class})
public class SituationServicesApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SituationServicesApplication.class, args);
}
@Override …Run Code Online (Sandbox Code Playgroud) 我有一个自定义异常类写为
public class MyCustomException extends RuntimeException {
public MyCustomException(Throwable cause) {
super(cause);
}
enter code here
/**
* @param message
* @param cause
*/
public MyCustomException(String message, Throwable cause) {
super(message, cause);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的serviceImpl层
@Override
public List<SiteDetails> getSite() throws MyCustomException {
}
Run Code Online (Sandbox Code Playgroud)
SONAR(用于 linting 的 Ecplise IDE 插件)指出:
删除抛出异常“.MyCustomException”的声明,这是一个运行时异常
我应该删除声明还是应该Exception在MyCustomException类中扩展而不是RunTimeException?
假设我有一个标题(对于一种类型的对象,所有页面都是通用的).
<%@ page session="false" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="header" uri="headerDir" %>
<%@ taglib prefix="tabs" uri="tabDir" %>
<div id="content" class="inside>
<header:myHeader headerData="${myModel}">
<tabs:myTabs argument="${someArg}"
....
Run Code Online (Sandbox Code Playgroud)
所以在我正在做的不同对象共享的视图中
<c:choose>
<c:when test="${myModel.type ==FIRST_TYPE}>
<header:myHeader headerData="${myModel}">
</c:when>
<c:otherwise>
<header:secondHeader headerData="${myModel}">
</c:otherwise>
<c:choose>
Run Code Online (Sandbox Code Playgroud)
但我想避免这种选择,我可以将此作为参数发送,因为我传递参数?我也可以这样做
<%@ page session="false" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="header" uri="headerDir" %>
<%@ taglib prefix="tabs" uri="tabDir" %>
<div id="content" class="inside>
<header:myHeader headerData="${myModel}">
<tabs:myTabs argument="${someArg}" headerToUse="${myHeader}" //in some way pass the header?
....
Run Code Online (Sandbox Code Playgroud) 可能重复:
尝试尽可能简单地描述多态
你如何定义polymorphismCS 101学生?尽可能简洁(可能是几行的答案,而不是太技术性).谢谢
我有以下用于Cucumber-jvm的步骤。如何转义步骤定义中的某些字符?
When user verifies if ABC widget exists
Then the 'The 7 Things $channel' label is displayed
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我需要将7和$作为常规字符串转义。
我有一张这样的地图,其中有数百万个条目:
private final Map<String, SomeItem> tops = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
我需要获取值列表,这可以通过调用 java.util.Map values()方法来完成。
每次调用方法时都会Collection创建值values(),还是从性能角度预先计算值?
由于我Map有几百万个元素,我不想每次values()调用时都创建新的列表对象。
帮助我,"线程中的异常"主"java.lang.NullPointerException"谢谢
private List< PosibleTerreno> posibles_terrenos;
private List< PosibleTerreno> terrenos_validos;
//-------------------------------
int cantidad = this.posibles_terrenos.size();
for (int i = 0 ; i < cantidad ; i++)
{
if(this.posibles_terrenos.get(i).get_validez() == true)
{
this.terrenos_validos.add(this.posibles_terrenos.get(i));
}
}
Run Code Online (Sandbox Code Playgroud) java ×8
api-design ×1
architecture ×1
arguments ×1
bdd ×1
collections ×1
cucumber ×1
dictionary ×1
escaping ×1
exception ×1
gherkin ×1
hashmap ×1
json ×1
jsonschema ×1
jsp ×1
memory ×1
pagination ×1
performance ×1
polymorphism ×1
ps ×1
servlets ×1
sonarlint ×1
spring ×1
spring-boot ×1
springfox ×1
swagger-2.0 ×1
swagger-ui ×1
text-editor ×1
xcode ×1