在下面的代码中,我注入了EnitityManager,它总是显示为null ;
public class GenericController extends AbstractController {
@PersistenceContext(unitName = "GenericPU")
private EntityManager em;
protected ModelAndView handleRequestInternal(
HttpServletRequest request,
HttpServletResponse response) throws Exception {
//(em == null) is always trigged
if (em == null) throw new NullPointerException("em is null");
Collection<Generic> Generics = em.createNamedQuery("Generic.findAll").getResultList();
ModelAndView mav = new ModelAndView("Generic");
mav.addObject(Generics);
return mav;
}
}
Run Code Online (Sandbox Code Playgroud)
这是bean定义,在dispatcher-servlet.xml中定义.
<bean id="Generic" class="com.application.web.GenericController" />
Run Code Online (Sandbox Code Playgroud)
应该在persistence-context.xml文件中定义基于tx:annotation的注入EnitityManager .
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean …Run Code Online (Sandbox Code Playgroud) 我在Python下使用NLTK遇到了麻烦,特别是.generate()方法.
生成(自我,长度= 100)
打印使用trigram语言模型生成的随机文本.
参数:
Run Code Online (Sandbox Code Playgroud)* length (int) - The length of text to generate (default=100)
这是我正在尝试的简化版本.
import nltk
words = 'The quick brown fox jumps over the lazy dog'
tokens = nltk.word_tokenize(words)
text = nltk.Text(tokens)
print text.generate(3)
Run Code Online (Sandbox Code Playgroud)
这将始终生成
Building ngram index...
The quick brown
None
Run Code Online (Sandbox Code Playgroud)
而不是建立一个随机短语.
这是我的输出
print text.generate()
Building ngram index...
The quick brown fox jumps over the lazy dog fox jumps over the lazy
dog dog The quick brown fox jumps over the lazy dog …Run Code Online (Sandbox Code Playgroud) 我是一名java开发人员,我想知道,学习Scala或Groovy等语言的主要好处是什么?
我对主键使用了以下 id 生成策略。
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false, insertable = false, updatable = false)
private Integer id;
Run Code Online (Sandbox Code Playgroud)
我想对非主键列做同样的事情。[a] 为这样的密钥定义自动生成方案的语义是什么 [b] 是否可以保证生成的数字不会出现数字差距。
我有两个清单:
listA:
[[Name: mr good, note: good,rating:9], [Name: mr bad, note: bad, rating:5]]
listB:
[[Name: mr good, note: good,score:77], [Name: mr bad, note: bad, score:12]]
Run Code Online (Sandbox Code Playgroud)
我想得到这个
listC:
[[Name: mr good, note: good,, rating:9, score:77], [Name: mr bad, note: bad, rating:5,score:12]]
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做?
谢谢.
无论如何,无论是本地还是通过库,在Javascript对象上使用自动生成?
IE,假设foo是一个没有属性的对象,能够做foo.bar.baz = 5而不是需要foo.bar = {}; foo.bar.baz = 5.
ExtJS 4.1.3中有没有办法在Grid上设置默认排序,后备存储设置为remoteSort: true?
我正在Grails 2.1.1下编写一些基于Spock规范的单元测试.我无法springSecurityService注入我单位使用的域对象.
这是我到目前为止,
@Mock([SecUser])
@TestFor(FooService)
class FooServiceSpec extends Specification {
def "test some stuff"() {
given:
def mockSecUserService = Mock(SecUserService)
mockSecUserService.emailValid(_) >> { true }
mockSecUserService.checkUsername(_) >> { null }
service.secUserService = mockSecUserService
def mockSpringSecurityService = Mock(SpringSecurityService)
mockSpringSecurityService.encodePassword(_) >> { 'tester' }
// FIXME this needs to be injected somehow
def params = [
email: 'unittest@test.com',
username: 'unittester',
password: 'tester',
password2: 'tester'
]
when:
def result = service.createUser(params)
then:
// test results
}
}
Run Code Online (Sandbox Code Playgroud)
所以会发生的事情是我的测试服务会抛出一个NullPointerException因为没有注入mockSpringSecruityService.我的服务中的createUser调用验证一些参数,然后创建一个SecUser …
有没有办法将Spring bean自动转换为应用程序上下文XML中定义的类?我想避免在两个位置放置关于bean的类型信息....在xml配置文件中以及在代码中作为强制转换.
例如,给定此配置文件
<bean id="bean-name" class="SimpleSpringBean" scope="prototype">
<property name="myValue" value="simple value"></property>
</bean>
Run Code Online (Sandbox Code Playgroud)
我可以这样调用ApplicationContext.getBean("bean-name")以避免直接将返回类型转换为SimpleStringBean.我知道我也可以打电话ApplicationContext.getBean("bean-name", SimpleSpringBean.class)来避免演员本身,但我仍然有2个地方的类型信息.
看起来Spring可以获取类info(ApplicationContext.getType)或者从bean本身获取类型,但没有办法在没有程序员干预的情况下自动转换类型.