我有一个变长的Java类.当我通过代码质量工具运行它时,我会被标记为类中的行数.
这是一个较低的层类,由上层使用Spring @Autowired
.该类有许多非静态的私有实例方法.它们不使用任何实例字段,仅适用于方法参数.
我可以像public static
在某个单独的实用程序类中一样安全地移动这些方 有什么缺点?
我是hibernate世界的新手,面对,
WARN: GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement
Run Code Online (Sandbox Code Playgroud)
我在hibernate 5.2.9版本中运行我的独立程序时出现异常.但是在hibernate 4版本中我的代码运行良好.我寻找了许多问题并解决了但我没有得到的答案.
配置文件
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.pool_size">20</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping class="com.test.hibernate14417.MyTable"></mapping>
</session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)
的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>Hibernate14417</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<repositories>
</repositories>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.9.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.1.Final</version>
</dependency> …
Run Code Online (Sandbox Code Playgroud) 我正在浏览这个链接,OBJ10-J.不要使用公共静态非最终字段,它说,
客户端代码可以简单地访问公共静态字段,因为安全管理器不会检查对这些字段的访问.
那是什么意思呢?即逃离安全经理他们是什么意思?
如果他们只是意味着它,因为现场被非最终和public
,然后怎么来的非最终,public
实例字段比他们不同static
的同行?(就代码安全而言)
我已经经历过这个问题并且在安全方面没有看到任何提及,为什么静态变量被认为是邪恶的
public
class的public static
字段可以从任何地方访问,所以public
实例字段也可以访问,那么区别在哪里?为什么非最终 public
实例字段不是安全问题,而是存在static
?
我不断从 Checkmarx 代码扫描仪收到这个烦人的错误,
Method getTotalValue at line 220 of src\java\com\example\PeopleController.java
gets user input for the personName element. This element’s value then flows through
the code without being properly sanitized or validated and is eventually
displayed to the user. This may enable a Cross-Site-Scripting attack.
Run Code Online (Sandbox Code Playgroud)
这是我的代码。我想我做了所有必要的验证。还有什么???
@Slf4j
@Configuration
@RestController
@Validated
public class PeopleController {
@Autowired
private PeopleRepository peopleRepository;
@RequestMapping(value = "/api/getTotalValue/{personName}", method = RequestMethod.GET)
@ResponseBody
public Integer getTotalValue(@Size(max = 20, min = 1, message = "person is not found")
@PathVariable(value="personName", required=true) …
Run Code Online (Sandbox Code Playgroud) 使用Spring Batch Admin时,它会尝试为dataSource,transactionManager等提供一些默认值.
如果要覆盖这些默认值,可以在META-INF/spring/batch/servlet/override /文件夹下创建自己的xml bean定义,并在引导期间保证将覆盖默认属性.
在spring-batch-admin中,dataSource缺省值在data-source-context.xml中使用此定义定义
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${batch.jdbc.driver}" />
<property name="url" value="${batch.jdbc.url}" />
<property name="username" value="${batch.jdbc.user}" />
<property name="password" value="${batch.jdbc.password}" />
<property name="testWhileIdle" value="${batch.jdbc.testWhileIdle}"/>
<property name="validationQuery" value="${batch.jdbc.validationQuery}"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
现在,我想用JNDI数据源覆盖这个dataSource,所以我删除了属性行batch.jdbc.driver
,batch.jdbc.url
并具有以下jndi定义
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/dbconn" />
</bean>
Run Code Online (Sandbox Code Playgroud)
您可能很容易猜到系统首先尝试初始化data-source-context.xml中定义的dataSource bean,因为它找不到属性值batch.jdbc.*的任何值,它会失败并出现异常.
无法在字符串值[$ {batch.jdbc.driver}]中解析占位符'batch.jdbc.driver'
由于我将使用JNDI并且不想处理这些属性值,所以我无法继续.
有关如何在这种情况下覆盖dataSource的想法?
我想在主弹簧引导类中传递JVM参数,我们将在其中启动spring引导应用程序.
你能否分享一下如何在spring boot应用程序中设置JVM参数?
我尝试了以下选项,没有运气
System.setProperty("http.proxyHost", "X.X.X.X");
System.setProperty("http.proxyPort", "8080");
Run Code Online (Sandbox Code Playgroud)
或者你可以用它来弹簧靴
bootRun {
systemProperties "property1": "value1", "property2": "value2"
}
Run Code Online (Sandbox Code Playgroud) 使用上下文路径时,我在 Spring Boot 中遇到静态内容问题。即:我希望将我的应用程序部署到localhost:8080/{appname}/
当我在没有上下文路径的情况下运行应用程序时,一切正常,Spring Boot 找到并运行我的.html
文件(来自资源/模板/,我正在使用Thymeleaf
)和 JS 文件(来自资源/静态/js/)但是当我添加上下文路径时与:
server.context-path=/{appname}
Run Code Online (Sandbox Code Playgroud)
或者
server.servlet-path=/{appname}
Run Code Online (Sandbox Code Playgroud)
那么.html
页面仍然显示由JS文件产生404错误。
我曾试图改变spring.resources.static-locations
的application.properties
并覆盖addResourceHandlers()
在我的方法,MvcConfig.class
但既不似乎工作
我使用一个MvcConfig
类是因为我需要定义 aCookieLocaleResolver
和 aMessageSource
但这就是我在MvcConfig
. 我不使用@EnableWebMvc
,只有@SpringBootApplication
带有@ComponentScan
.
任何帮助,将不胜感激 :)
在方法接口的API文档中,粘贴此代码示例,Lock
tryLock()
此方法的典型用法习惯是:
Run Code Online (Sandbox Code Playgroud)Lock lock = ...; if (lock.tryLock()) { try { // manipulate protected state } finally { lock.unlock(); } } else { // perform alternative actions }
我的问题是,这个用例在Java 5之前是不存在的还是人们通过其他技术来实现它?
我无法理解是否需要根据锁定可用性执行替代操作.
有人可以为此解释实际用例吗?
我确信这种技术不是直接替代synchronized
编写无死锁代码.
我使用Spring引导1.5.2.RELEASE并不能纳入JSR - 349(Bean验证1.1),用于@RequestParam
与@PathVariable
在方法本身.
对于POST请求,如果方法参数是Java POJO,那么注释该参数与@Valid
工作正常但注释@RequestParam
和@PathVariable
类似@NotEmpty
,@Email
不工作.
我有Spring的注释控制器类 @Validated
关于SO有很多问题,我对这个答案发表了评论,认为它不适用于我.
Spring Boot包括 - validation-api-1.1.0.Final.jar
和hibernate-validator-5.3.4.Final.jar
.
我错过了什么吗?
示例代码,
@RequestMapping(method = RequestMethod.GET, value = "/testValidated", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseBean<String> testValidated(@Email @NotEmpty @RequestParam("email") String email) {
ResponseBean<String> response = new ResponseBean<>();
response.setResponse(Constants.SUCCESS);
response.setMessage("testValidated");
logger.error("Validator Not called");
return response;
}
Run Code Online (Sandbox Code Playgroud)
下面的处理程序永远不会被调用当我发送空值或者没有良好形成的电子邮件地址时,email
&control总是用in testValidated
方法.
@ExceptionHandler(ConstraintViolationException.class)
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseBean handle(ConstraintViolationException exception) {
StringBuilder …
Run Code Online (Sandbox Code Playgroud) 我保存了一些会话属性.我有一个jsp页面,通过它调用servlet.这个servlet更新了一个会话变量,但是我无法在jsp.Pls帮助中看到这些变化的反映.
在我的servlet中
List<DriverList> abc = dao.getABC();
request.getSession().removeAttribute("abc");
request.getSession().setAttribute("abc", abc);
Run Code Online (Sandbox Code Playgroud)
在我的jsp中
function update()
{
var url = "updateServlet";
var req = $.ajax({
type: 'GET',
url: url,
cache: false,
type: "GET",
success: function()
{
latlng = [];
latlng = [<c:forEach var="test" items="${abc}">
[<c:out value="${test.latitude}"/>,<c:out value="${test.longitude}"/>,"<c:out value= "${test.name}" />",<c:out value="${test.cellNo}"/>],
</c:forEach> ];
},
error: function (status) {
}
});
}
Run Code Online (Sandbox Code Playgroud)
值${abc}
与以前相同.如何获得新价值?
确切的流量 -
当登录servlet被调用abc
为sessionAttribute
设置值时.
现在重定向到base.jsp.我第一次使用abc.现在每30秒后update()
调用一次该函数.此更新函数通过ajax调用servlet,其中会话属性abc已更新.
在ajax请求的成功函数中,我想使用这个新的abc值但是再次获取旧的值.
java ×7
spring ×4
spring-boot ×4
spring-mvc ×2
checkmarx ×1
concurrency ×1
hibernate ×1
http-get ×1
jdbc ×1
jpa ×1
jsp ×1
jsr ×1
locking ×1
servlets ×1
session ×1
spring-batch ×1
spring-data ×1
static ×1
validation ×1