我想在运行时重新创建(新对象)特定的bean(在没有重新启动服务器的情况下)进行一些数据库更改.这就是它的样子 -
@Component
public class TestClass {
@Autowired
private MyShop myShop; //to be refreshed at runtime bean
@PostConstruct //DB listeners
public void initializeListener() throws Exception {
//...
// code to get listeners config
//...
myShop.setListenersConfig(listenersConfig);
myShop.initialize();
}
public void restartListeners() {
myShop.shutdownListeners();
initializeListener();
}
}
Run Code Online (Sandbox Code Playgroud)
此代码不会运行,因为myShopSpring将作为Singleton创建对象,并且除非重新启动服务器,否则它的上下文不会刷新.如何刷新(创建一个新对象)myShop?
我能想到的一个坏方法是在myShop里面创建新对象,restartListeners()但这对我来说似乎并不合适.
JGoodies Binding和JSR 295,Beans Binding有什么实际区别?它们似乎都是出于同样的目的而完成工作(采用略有不同的方法).JGoodies Binding更加成熟,但JSR 295最终将成为Java 7中JDK的一部分.
使用JDK的标准部分肯定比使用单独的库来实现相同的功能更好,但还有其他因素需要考虑吗?还有其他一些选择吗?在这些之间进行选择是相当不可逆转的架构决策......
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("abc", "123456");
map.put("def", "hmm");
list.add(map);
JSONObject json = new JSONObject(list);
try {
System.err.println(json.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
这段代码出了什么问题?
输出是:
{"empty": false}
Run Code Online (Sandbox Code Playgroud) 我有一个简单的Java POJO,我将属性复制到同一个POJO类的另一个实例.
我知道我可以用BeanUtils.copyProperties()做到这一点,但我想避免使用第三方库.
那么,如何简单地,正确和安全的方式呢?
顺便说一下,我正在使用Java 6.
我对@Injectjava ee 6中的注释有疑问:
有什么区别:
@Inject
private TestBean test;
@Inject
private Instance<TestBean> test2;
Run Code Online (Sandbox Code Playgroud)
要参考:
test2.get();
Run Code Online (Sandbox Code Playgroud)
关于Instance的一些信息:http://docs.oracle.com/javaee/6/api/javax/enterprise/inject/Instance.html
也许在get()调用之前它不会创建对象?我只是想知道哪一个更适合jvm内存.我认为direct @Inject会直接创建一个对象的实例,即使它没有被appplication使用...
谢谢 !
我查看了文档来定义bean.我只是不清楚用于Mysql数据库的类文件.任何人都可以填写下面的bean定义吗?
<bean name="dataSource" class="">
<property name="driverClassName" value="" />
<property name="url" value="mysql://localhost/GameManager" />
<property name="username" value="gamemanagertest" />
<property name="password" value="1" />
</bean>
Run Code Online (Sandbox Code Playgroud) 当我创建一个弹簧项目时,我总是遇到XLMNS的问题.什么是XMLNS?实际上这些是什么?
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
Run Code Online (Sandbox Code Playgroud)
我在哪里可以获得这些参考?(xmlns的资源:xsi和xsi:schemeLocation.)是否有这些在线手册?我似乎无法找到它们.
注意 当我说引用时,我指的是适合它们的URL
更新
我在哪里可以看到Spring bean,Spring Transactions,Spring MVC等的XML命名空间?及其架构位置?
据我所知,field injection不推荐.应该使用constructor.
我在这里尝试做的是@Autowired在基类的构造函数中使用,并使其可供所有子类访问.在某些子类中,我还需要一些特定的bean @Autowired来自它们的构造函数.演示代码如下:
基类:
public abstract class Base {
protected final MyDemoService myDemoService;
@Autowired
public Base(MyDemoService myDemoService) {
this.myDemoService = myDemoService;
}
}
Run Code Online (Sandbox Code Playgroud)
继承(子)类:
public class Sub extends Base {
private final MySubService mySubService;
@Autowired
public Sub(MySubService mySubService) {
this.mySubService = mySubService;
}
}
Run Code Online (Sandbox Code Playgroud)
这将给我一个'无默认构造函数'错误.
它类似于问题:类似的问题和答案,但它在这里不起作用.
我已经潜入了一段时间,我发现这篇文章dependency injection:进一步阅读
我在想Setter Injection我的问题是正确的方法吗?
塞特犬注射:
public abstract class Base {
protected MyDemoService myDemoService;
@Autowired
public void setMyDemoService(MyDemoService myDemoService) { …Run Code Online (Sandbox Code Playgroud) 我对Interface和BeanInfo Introspector中的默认方法有一点问题.在这个例子中,有接口:Interface
public static interface Interface {
default public String getLetter() {
return "A";
}
}
Run Code Online (Sandbox Code Playgroud)
以及ClassA和ClassB两个类:
public static class ClassA implements Interface {
}
public static class ClassB implements Interface {
public String getLetter() {
return "B";
}
}
Run Code Online (Sandbox Code Playgroud)
在main方法应用程序中打印来自BeanInfo的PropertyDescriptors:
public static String formatData(PropertyDescriptor[] pds) {
return Arrays.asList(pds).stream()
.map((pd) -> pd.getName()).collect(Collectors.joining(", "));
}
public static void main(String[] args) {
try {
System.out.println(
formatData(Introspector.getBeanInfo(ClassA.class)
.getPropertyDescriptors()));
System.out.println(
formatData(Introspector.getBeanInfo(ClassB.class)
.getPropertyDescriptors()));
} catch (IntrospectionException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
结果是:
class
class, letter …Run Code Online (Sandbox Code Playgroud) javabeans ×10
java ×8
spring ×4
binding ×1
cdi ×1
datasource ×1
definition ×1
ejb ×1
interface ×1
java-8 ×1
java-ee-6 ×1
json ×1
map ×1
singleton ×1
spring-boot ×1