在我的main()方法中,我使用Spring创建一个PersonCollection对象,然后我开始加载不同的Persons对象.
BeanFactory appContext = new ClassPathXmlApplicationContext("cp-beans.xml");
PersonCollection pc = appContext.getBean(PersonCollection.class);
Person aPerson = pc.loadById(1);
aPerson.doSomething();
aPerson.loadById(1067);
aPerson.doSomething();
Run Code Online (Sandbox Code Playgroud)
反过来,PersonCollection.loadById()可以从memcached或Amazon SimpleDB加载对象:
public Person loadById(int id) throws ConnectException, NoSuchElementException {
String memCacheKey = "Person-" + id;
Person aPerson = (Person) cache.get(memCacheKey);
if (aPerson != null) {
return aPerson; //cache hit
}
aPerson = loadByIdFromSdb(id); //cache miss, read it from SimpleDB
cache.set(memCacheKey, aPerson);
return aPerson;
}
Run Code Online (Sandbox Code Playgroud)
因此有两种方法可以创建Person,第一种是从memcached反序列化,第二种是调用new Person()并分配所有数据.
Person有两个@Autowired属性并被声明为@Service,并且包在上下文中:component-scan,但是不传递依赖项,因为bean是使用new创建的,或者是从缓存而不是使用Spring框架创建的.
我可以使用appContext.getBean()来创建Person对象,但是,它意味着传递applicationContext并在应用程序中使用getBean(),这感觉不对.
如何解决问题?
更新:我阅读了文档并尝试了Ryan Stewart的建议,并编写了一个小示例项目来尝试它.它很棒,谢谢!
https://github.com/stivlo/spring-di
最终,我已经重构了我原来的项目,我不再需要这个功能,但是在我的武器库里有一个很好的工具.
我使用的是Spring 3,我在applicationContext.xml中有以下配置:
<bean id="validationMessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:basename="classpath:messages/validation_messages" p:defaultEncoding="UTF-8" p:cacheSeconds="3" />
<bean id="globalValidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource">
<ref bean="validationMessageSource" />
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
一切都可以正常使用locales等.
但是,我想知道是否可以将语言环境注入我构建的自定义验证器.我已经创建了一个@CheckZip用于验证邮政编码的注释.但由于邮政编码在不同的国家/地区有不同的格式,我很好奇是否可以将当前的语言环境放入验证器中.
我有一个长时间运行的计时器任务.它实际上是一个批处理过程,它将永远循环并运行它的东西.该批处理程序是用Java编写的.现在我如何要求它优雅地关闭,例如我需要做一些维护?从JVM外部?
我可以想到一些肮脏的方法,在run方法的每个循环结束时,让它检查某个目录中是否存在文件.如果存在则会停止.或者创建一个数据库记录,要求它停止.
还有另一种更好的方法吗?
在运行以下代码时,我收到错误java.lang.OutOfMemoryException : java heap space
我的代码是:
public class openofficeupdate {
String databaseurl="C:\\mydbdir\\location\\salesforce"; // Path of the base after renaming and extraction
openofficeupdate() throws ClassNotFoundException, SQLException{
System.out.println("Entered into constructor");
Connection connection=null;
Statement statement=null;
try{
Class c=openofficeclass();
System.out.println("Class name set");
Connection cntn=createConnection(databaseurl);
connection=cntn;
System.out.println("connection created");
Statement stmt=createStatement(cntn);
statement=stmt;
System.out.println("Statement created");
executeQueries(stmt);
System.out.println("Query executed");
closeStatement(stmt);
System.out.println("Statement closed");
closeConnection(cntn);
System.out.println("Connection closed");
}catch(Exception e){
System.out.println(e);
closeStatement(statement);
System.out.println("Statement closed");
closeConnection(connection);
System.out.println("Connection closed");
}
}
public static void main(String args[]) throws ClassNotFoundException, SQLException{
new openofficeupdate();
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试学习Hibernate,我编写了最简单的Person Entity,我试图插入2000个.我知道我正在使用弃用的方法,我将在稍后尝试找出新的方法.
首先,这是类Person:
@Entity
public class Person {
private int id;
private String name;
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "person")
@TableGenerator(name = "person", table = "sequences", allocationSize = 1)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我写了一个小的App类,用循环插入2000个实体:
public class App {
private static AnnotationConfiguration config;
public static void insertPerson() {
SessionFactory factory = …Run Code Online (Sandbox Code Playgroud) 我正在通过阅读文档来学习Symfony和Doctrine .
我不明白find和findOneById之间的区别.我试着在这个简单的例子中使用它们,看起来它们对我做同样的事情.
$product = $this->getDoctrine()
->getRepository('AcmeStoreBundle:ProductEntity')
->findOneById($id);
Run Code Online (Sandbox Code Playgroud)
它们真的是一样的还是有区别的?在哪里可以找到所有这些方法的详细文档?
我一直试图在最后两个小时解决这个问题,但它只是不起作用:(
我已经下载了一个网页的html代码,然后我删除了所有双白空格和所有新行,所以整个代码是一行字符串.
然后我必须从中提取一个数据
page.com/users/(this)/xxxxx/.....
match = Regex.Match(htmlCode, "page.com/users/(.*)/xxxxx/");
string user = match.Groups[1].ToString();
Run Code Online (Sandbox Code Playgroud)
但它不起作用,我总是得到(这个)/ xxxxx/+其余的html代码.
任何人都知道为什么这不起作用?
int main(){
fork();
}
Run Code Online (Sandbox Code Playgroud)
我知道这是一个新手问题,但我的理解是,父进程现在将新的子进程分叉为父进程,这意味着子进程也应该分叉子进程等等...实际上,这个只生成一个子进程.我不明白孩子会执行什么代码?
要使用Symfony2以编程方式在表单中设置值,在创建表单后,我发现以下方式有效:
$form_data = $form->getData();
$form_data['name'] = 'new value';
$form->setData($form_data);
Run Code Online (Sandbox Code Playgroud)
这很尴尬,是不是有更简单的方式$form->set('name', 'new value');?
什么是从视图到控制器获取用户输入的最佳方法.我的意思是特定输入不像"FormCollection"那样像"对象人"或"int值"以及如何在特定间隔刷新页面
例如,我已进入
使用Netsh.exe
在命令行(又名cmd.exe)
现在我想知道正在运行哪个netsh.exe,假设我的PATH上有多个netsh.exe(我知道PATH中的第一个将会运行,但是让我说我有一个非常大的路径和我没有时间手动搜索它.公平地说,它并不总是你正在使用的机器,很多时候PATH由管理员设置,很多时候它们不是最好的).
在Windows中是否有任何方法可以从命令行找到它?我想编写一个使用它的BATCH应用程序.
如何在Perl中获取当前计算机主机名?我正在寻找一种适用于Linux和Windows的方法.
使用websearch我找到了模块Sys :: Hostname,但我无法安装它
install Sys::Hostname
Going to read '/home/stivlo/.cpan/Metadata'
Database was generated on Sat, 04 Jun 2011 15:27:16 GMT
Running install for module 'Sys::Hostname'
The most recent version "1.16" of the module "Sys::Hostname"
is part of the perl-5.13.11 distribution. To install that, you need to run
force install Sys::Hostname --or--
install F/FL/FLORA/perl-5.13.11.tar.gz
Running make test
Can't test without successful make
Running make install
Make had returned bad status, install seems impossible
Failed during this command:
FLORA/perl-5.13.11.tar.gz : make NO …Run Code Online (Sandbox Code Playgroud) 我目前面临一个奇怪的问题.我正在测试网站响应时间,但是当第三次(连接计时)方法循环时,它会挂起:
internal class Program
{
Console.ReadLine();
loop();
}
}
Run Code Online (Sandbox Code Playgroud)
它挂起之前的输出是:"HTTP Response Timer",所以我认为它与正在运行的实例有关.