我最近从log4j切换到logback,我想知道是否有一种简单的方法在调试模式下运行logback,类似于log4j的log4j.debug属性.我需要看看它logback.xml从哪里拿起来.
文档提到使用a StatusPrinter打印出logback的内部状态,但这需要更改代码.
我试图使我的 <hr />(hr)元素变粉红色,并使用以下css规则:
hr {height: 1px; color: #ed1d61;background-color: #ed1d61;
}
Run Code Online (Sandbox Code Playgroud)
但是仍然有一条黑线显示出来.
(以下是我正在制作的网站上的内容:http://www.yemon.org/,它是设计中唯一的水平线.
我如何让线条均匀粉红色?
有什么区别:
cmd > log 2>&1
Run Code Online (Sandbox Code Playgroud)
和
cmd 2>&1 > log
Run Code Online (Sandbox Code Playgroud)
cmd是命令吗?
我应该选择哪个?为什么?
如何将一个shell脚本的所有参数传递给另一个?我已经尝试了$*,但正如我所料,如果你引用了参数,那就不行了.
例:
$ cat script1.sh
#! /bin/sh
./script2.sh $*
$ cat script2.sh
#! /bin/sh
echo $1
echo $2
echo $3
$ script1.sh apple "pear orange" banana
apple
pear
orange
Run Code Online (Sandbox Code Playgroud)
我希望它打印出来:
apple
pear orange
banana
Run Code Online (Sandbox Code Playgroud) 我正试图权衡使用EnumMapa的优缺点HashMap.因为,我将一直在寻找使用a String,似乎HashMap用一把String钥匙将是正确的选择.然而,EnumMap似乎更好的设计,因为它传达了我将密钥限制为特定枚举的意图.思考?
这是一个虚构的例子,展示了我将如何使用Map:
enum AnimalType { CAT, DOG }
interface Animal {}
class Cat implements Animal {}
class Dog implements Animal {}
public class AnimalFactory {
private static final Map<AnimalType, Animal> enumMap
= new EnumMap<AnimalType, Animal>(AnimalType.class);
// versus
private static final Map<String, Animal> stringMap
= new HashMap<String, Animal>();
static {
enumMap.put(AnimalType.CAT, new Cat());
enumMap.put(AnimalType.DOG, new Dog());
stringMap.put("CAT", new Cat());
stringMap.put("DOG", new Dog());
}
public static Animal create(String …Run Code Online (Sandbox Code Playgroud) 我想将一个依赖注入一个ServletContextListener.但是,我的方法不起作用.我可以看到Spring正在调用我的setter方法,但是稍后在contextInitialized调用时,属性是null.
这是我的设置:
ServletContextListener:
public class MyListener implements ServletContextListener{
private String prop;
/* (non-Javadoc)
* @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
*/
@Override
public void contextInitialized(ServletContextEvent event) {
System.out.println("Initialising listener...");
System.out.println(prop);
}
@Override
public void contextDestroyed(ServletContextEvent event) {
}
public void setProp(String val) {
System.out.println("set prop to " + prop);
prop = val;
}
}
Run Code Online (Sandbox Code Playgroud)
web.xml :(这是文件中的最后一个监听器)
<listener>
<listener-class>MyListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
applicationContext.xml中:
<bean id="listener" class="MyListener">
<property name="prop" value="HELLO" />
</bean>
Run Code Online (Sandbox Code Playgroud)
输出:
set prop to HELLO
Initialising listener...
null
Run Code Online (Sandbox Code Playgroud)
实现这一目标的正确方法是什么?
是否可以忽略使用try-with-resources语句关闭资源时抛出的异常?
例:
class MyResource implements AutoCloseable{
@Override
public void close() throws Exception {
throw new Exception("Could not close");
}
public void read() throws Exception{
}
}
//this method prints an exception "Could not close"
//I want to ignore it
public static void test(){
try(MyResource r = new MyResource()){
r.read();
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
或者我应该继续关闭finally?
public static void test2(){
MyResource r = null;
try {
r.read();
}
finally{
if(r!=null){
try {
r.close(); …Run Code Online (Sandbox Code Playgroud) 我想使用其名称创建指定类的实例.我的代码如下所示.
我收到编译器警告.我这样做是对的吗?甚至可以使用类的名称并获取该类型的实例,因为我认为编译器不知道该类型应该是什么方式?
public static <T> T create(final String className) {
try {
final Class<?> clazz = Class.forName(className);
//WARNING: Type safety: Unchecked cast from capture#2-of ? to T
return (T) create(clazz);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static <T> T create(final Class<T> classToCreate) {
final Constructor<T> constructor;
try {
constructor = classToCreate.getDeclaredConstructor();
final T result = constructor.newInstance();
return result;
}
catch (Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
在以前的java版本中,重新抛出异常被视为抛出catch参数的类型.
例如:
public static void test() throws Exception{
DateFormat df = new SimpleDateFormat("yyyyMMdd");
try {
df.parse("x20110731");
new FileReader("file.txt").read();
} catch (Exception e) {
System.out.println("Caught exception: " + e.getMessage());
throw e;
}
}
Run Code Online (Sandbox Code Playgroud)
在Java 7中,如果声明异常,则可以更精确地了解抛出的异常final:
//(doesn't compile in Java<7)
public static void test2() throws ParseException, IOException{
DateFormat df = new SimpleDateFormat("yyyyMMdd");
try {
df.parse("x20110731");
new FileReader("file.txt").read();
} catch (final Exception e) {
System.out.println("Caught exception: " + e.getMessage());
throw e;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题:文档说我需要声明异常final.但如果我不这样做,上面的代码仍然编译和工作.我错过了什么吗?
参考文献:
我想在字符串中对字符进行排序.
例如
echo cba | sort-command
abc
Run Code Online (Sandbox Code Playgroud)
是否有一个允许我这样做的命令,或者我是否必须编写一个awk脚本来迭代字符串并对其进行排序?