小编G.S*_*sky的帖子

如何将Maven配置文件值注入属性文件

有点卡在这里.我有一个有3个配置文件的pom.Theese个人资料有不同的版本名称.我想在构建特定的配置文件时将该版本名称注入属性文件中.

我的档案:

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <projectVersion>DEV</projectVersion>
        </properties>
    </profile>

    <profile>
        <id>test</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <projectVersion>1.0.0-RC1</projectVersion>
        </properties>
    </profile>

    <profile>
        <id>prod</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <projectVersion>1.0.0-Final</projectVersion>
        </properties>
    </profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)

和filter.properties看起来像这样:

projectName = defaultName
versionName = defaultVersion
Run Code Online (Sandbox Code Playgroud)

怎么做?我按命令建立项目:

mvn clean install -D profile_name
Run Code Online (Sandbox Code Playgroud)

java maven

6
推荐指数
2
解决办法
6126
查看次数

在Maven中替换文件的正确方法是什么?

我有3个不同的配置文件的Maven应用程序,如下所示

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profileVersion>DEV</profileVersion>
                <webXmlFolder>${id}</webXmlFolder>
            </properties>
        </profile>

        <profile>
            <id>test</id>
            <properties>
                <profileVersion>1.0.0-RC1</profileVersion>
                <webXmlFolder>${id}</webXmlFolder>
            </properties>
        </profile>

        <profile>
            <id>prod</id>
            <properties>
                <profileVersion>1.0.0-Final</profileVersion>
                <webXmlFolder>${id}</webXmlFolder>
            </properties>
        </profile>
    </profiles>
Run Code Online (Sandbox Code Playgroud)

我有这样的Maven结构:

的src/main /配置/默认/ WEB-INF/web.xml文件

的src /主/配置的/ dev/WEB-INF/web.xml中

的src /主/配置/测试/ WEB-INF/web.xml中

的src /主/配置/ PROD/WEB-INF/web.xml中

的src /主/ web应用/ WEB-INF /

我的任务是在构建时将指定的web.xml设置为webapp/WEB-INF,具体取决于指定的配置文件.如果未指定配置文件,则web.xml将从默认文件夹进行复制.

我有插件,但它无法正常工作.

    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <id>copy-prod-resources</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <overwrite>true</overwrite>
                        <outputDirectory>${project.build.outputDirectory}/classes/WEB-INF</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/config/${webXmlfolder}/WEB-INF</directory>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?我花了很多时间处理这个问题,现在我很困惑.

java maven

6
推荐指数
1
解决办法
4097
查看次数

如何在Spring Boot中覆盖Spring Security默认配置

我对Spring Boot有一点问题。我真的很喜欢Spring Boot,它是一个非常方便的工具,它使我可以专注于逻辑实现而不是bean配置。但是...我想覆盖默认的Spring Security配置。我已经创建了SecurityConfig类,并且Spring Boot在启动时会加载该类。我可以在地址/ bean上看到我的配置bean。

但是配置仍然是默认设置(我想)。

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{
        auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
    }

    protected void cofigure(HttpSecurity http) throws Exception{

        http.authorizeRequests()
        .antMatchers("/app/").permitAll()
        .anyRequest().authenticated()
        .and()
        .formLogin()
        .loginPage("/login")
        .permitAll()
        .and()
        .logout()
        .permitAll()
        .and()
        .csrf()
        .and()
        .exceptionHandling()
        .accessDeniedPage("/error");
    }
}
Run Code Online (Sandbox Code Playgroud)

我用用户名:user声明了用户,并通过了密码。我还声明,Spring Security必须允许任何用户看到索引站点:localhost:port / app /。

我以为如果我在浏览器中输入localhost:port / app / url,Spring Security会让我进入。相反,我得到了localhost:port / login页面和默认的Spring Security登录表单。此外,在AuthenticationManager中声明的用户名和密码无效。

但是,如果我输入我的应用程序属性凭据,它将起作用。

security.user.name=testUser
security.user.password=testPass
security.user.role=USER
Run Code Online (Sandbox Code Playgroud)

结论:Spring Boot似乎加载了我的自定义Spring Securty配置,但没有使用它。

为什么?

编辑:

现在我的SecurityConf看起来像这样:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security spring-boot

5
推荐指数
1
解决办法
1万
查看次数

Hibernate和MySQL配置

我花了几个小时试图设置我的第一个Hibernate应用程序,它仍然无法正常工作.

我的WAMP服务器和我的MySQL数据库名为"hibernatetest".我在Eclipse中有Project,它包含Hibernate库和mysql-connector-java-5.1.18-bin.jar.我也有这个课程:

HibernateUtil.java:

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure().buildSessionFactory(
                new StandardServiceRegistryBuilder().build() );
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}
Run Code Online (Sandbox Code Playgroud)

test.java(包含main):

import org.hibernate.Session;

import …
Run Code Online (Sandbox Code Playgroud)

java mysql eclipse hibernate hibernateexception

4
推荐指数
1
解决办法
3万
查看次数

JUnit - 针对不同实现的一种测试

我在为我的程序编写测试时遇到了一个小问题。我有一个 SortingAlgorithm 接口,还有一些实现,如 BubbleSort、InsertionSort、QuickSort……

我不想为这些 SortingAlgorithms 实现中的每一个都创建一个测试用例。我想将每个算法类注入一个 TestCase,然后分别为每个算法运行 TestCase。

怎么做?

我的代码:

public class SortingAlorithmTest {

SortingAlgorithm sortingAlgorithm;
final int amount = 50000;
final int delayTime = 0;
int[] numbers;

public SortingAlorithmTest(SortingAlgorithm sortingAlgorithm){
    this.sortingAlgorithm = sortingAlgorithm;
}

@Before
public void setUp() throws Exception {
    Random random = new Random();
    numbers = new int[amount];

    for (int i = 0; i < amount; i++){
        numbers[i] = random.nextInt(Preferences.numberScope);
    }

    AlgorithmDelayer.setDelayTime(delayTime);
}

@Test(expected = NullPointerException.class)
public void passingNullValueTest(){
    sortingAlgorithm.sort(null);
}

@Test(timeout = 1000)
public void …
Run Code Online (Sandbox Code Playgroud)

java junit

4
推荐指数
1
解决办法
205
查看次数

例外或无效签入?什么更优雅?

假设我有html表单和控制器来处理该表单.Controller启动一个服务,它接受2个参数.如果用户发送空字段,则参数可以为null.

现在......如何处理这种情况?什么更优雅?我更喜欢将此值从控制器传递到服务,而不检查控制器层中的值是否为null.

第一种方法 - 检查Controller层的空值

@RequestMapping(method = RequestMethod.POST)
    public String borrowBook(@RequestParam(value = "borrower_id", required = false) Long borrowerId,
            @RequestParam(value = "book_id", required = false) Long bookId){

        if (borrowerId != null && bookId != null)
            borrowService.createBorrow(bookId, borrowerId);

        return "redirect:index.html";
    }
Run Code Online (Sandbox Code Playgroud)

第二种方法 - 在cotnroller层将值传递给service和cheking exception

@RequestMapping(method = RequestMethod.POST)
    public String borrowBook(@RequestParam(value = "borrower_id", required = false) Long borrowerId,
            @RequestParam(value = "book_id", required = false) Long bookId){

        try {
            borrowService.createBorrow(bookId, borrowerId);
        } catch(CreateBorrowException e){
            //Do something
        }

        return "redirect:index.html";
    }
Run Code Online (Sandbox Code Playgroud)

第三种方法 - 在服务层处理异常,因此控制器实际上不知道是否发生异常. …

java

2
推荐指数
1
解决办法
82
查看次数

Java 2D 游戏有多少个线程?

我是新来的,我学习java已经两个月了。我实际上正在学习线程和多线程,我有一个小问题。为了练习我编写一个简单的 2D 卡牌游戏(如 MTG 或 HearthStone)。为了这一刻我做了很多事情,但我想知道我必须使用多少线程才能创建最高效的应用程序并养成良好的习惯。

因此,现在我在 EventQueue 中有 JFrame,以及扩展 JPanel 和实现 Runnable 的其他类,这是我的 Board,并且具有游戏循环(使用 init()、uptade() 和 repaint() 以及鼠标侦听器方法)。

对于简单的游戏来说有这么好吗?或者也许板上的每张卡片都应该有一个单独的线程来显示卡片信息、重新绘制等?

我感谢每一个帮助,干杯!

java swing multithreading 2d-games

1
推荐指数
1
解决办法
1473
查看次数

如何将H2控制台连接到嵌入式Spring H2 DB

好的,我正在开发简单的应用程序,它有Spring Ebedded H2数据库用于开发.database.xml bean conf看起来像这样:

<bean id="h2Server" class="org.h2.tools.Server" factory-method="createTcpServer"
        init-method="start" destroy-method="stop" depends-on="h2WebServer">
        <constructor-arg value="-tcp,-tcpAllowOthers,-tcpPort,9092" />
    </bean>
    <bean id="h2WebServer" class="org.h2.tools.Server" factory-method="createWebServer"
        init-method="start" destroy-method="stop">
        <constructor-arg value="-web,-webAllowOthers,-webPort,8082" />
    </bean>

    <jdbc:embedded-database id="dataSource" type="H2" />
Run Code Online (Sandbox Code Playgroud)

H2数据库正在初始化,我的应用程序正在运行,我正在创建实体,并且它们在Tomcat启动时存储在H2 db中(我知道它因为我使用并检索它们).但是,当我查看H2控制台时,我的实体表不存在.

我猜H2控制台指向另一个H2数据库,而Spring Embedded H2 Db与那个H2控制台无关.

如何解决?

编辑:我通过在我的网络浏览器中输入http:// localhost:8082来访问H2控制台.

java database spring spring-mvc h2

1
推荐指数
1
解决办法
2982
查看次数

如何在paintComponent方法中为图像添加颜色图层?

我用Java编写了我的第一个游戏.所以我有Board类扩展JPanel,在这个类里面我有paintComponent方法.卡片图像是BufferedImages.

现在......我刚刚完成方法,计算可能的球员动作.为了澄清,我有敌人卡片场,并且每当敌人的CardField在玩家移动时我想向敌人的cardField图像添加一些图层.

我在paintComponent方法中的代码是这样的:

if(!field.isWithinMove()){
//draw normal state card
  g.drawImage(field.getLoadedCard().getCardImage(),
  field.getX(), field.getY(), Card.cardWidth, Card.cardHeight, null);
}
else{
 //there should be a card picture with layer of color 
}
Run Code Online (Sandbox Code Playgroud)

我的目标是:

正常:

正常

精彩推荐:

在此输入图像描述

我将非常感谢你的帮助:)

干杯!

java swing image

0
推荐指数
1
解决办法
229
查看次数

HTML - 为什么隐藏="false"的元素不可见

我在freemarker中有一个观点.当我加载我的模板时,我正在检查一个值是否为空.如果是,那么我想让我的div隐藏在开始.如果不是,那么我想让我div看得见.有人能告诉我,为什么这种尝试不起作用?

<div class="..." hidden="false"></div>
Run Code Online (Sandbox Code Playgroud)

无论 hidden 是真还是假,div都是不可见的.为什么?

完整代码示例:

<#if errorMessageNameList??>
    <#assign hideForm="false" />
<#else>
    <#assign hideForm="true" />
</#if>
<div class="saveEquipmentPanel" hidden="${hideForm}">
    <@saveEquipmentPanel />
</div>
Run Code Online (Sandbox Code Playgroud)

html css

0
推荐指数
1
解决办法
5703
查看次数