如果支持XML和注释,spring框架执行注释扫描以及如何处理它?
有人可以给出细节情景吗?
对于XmlWebApplicationContext,它通过Xml文件使用loadBeanDefinations().但是当它进行注释扫描并使用哪个类来处理它?
可以解释一下细节吗?
谢谢.
富兰克林
我正在尝试使用hibernate注释实现我的模型.我有3个班级,图像,人物和标签.标签是由4个字段组成的表,id,personId,imageId和createdDate.Person有字段名,id,birthdate等.我的图像类定义如下:
@Entity
@Table(name="Image")
public class Image {
private Integer imageId;
private Set<Person> persons = new HashSet<Person>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
public Integer getImageId() {
return imageId;
}
public void setImageId(Integer imageId) {
this.imageId = imageId;
}
@ManyToMany
@JoinTable(name="Tags",
joinColumns = {@JoinColumn(name="imageId", nullable=false)},
inverseJoinColumns = {@JoinColumn(name="personId", nullable=false)})
public Set<Person> getPersons() {
return persons;
}
public void setPersons(Set<Person> persons) {
this.persons = persons;
}
Run Code Online (Sandbox Code Playgroud)
如果我删除了getPersons()方法上的注释,我可以使用这些类并添加和删除记录.我想用图像获取所有标签,我正在尝试使用一组.我一直收到以下错误:
org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: com.exmaple.persons, no session or …Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
import java.util.Collections;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.User;
public class SecureStuff {
@PreAuthorize("#user.password == #oldPassword")
public static void changePassword(User user, String oldPassword, String newPassword){
System.out.print("Changing passwords ...");
}
public static void main(String[] args) {
User joe = new User("Joe", "HansWurst", true, true, true, true, Collections.EMPTY_LIST);
changePassword(joe, "HansWurst", "TeeWurst");
}
}
Run Code Online (Sandbox Code Playgroud)
我在STS(SpringSource工具套件)中运行代码,它按预期工作.(它打印出来"Changing passwords ...".)然后我将密码重命名为其他东西,期待方法调用现在失败.
我已将该行添加<global-method-security pre-post-annotations="enabled"/>到applicationContext-security.xml配置文件中.
我在这里错过了什么?
引用此链接:
一些开发人员认为Java编译器理解标记并相应地工作.这个不对.标签实际上对Java编译器或运行时本身没有任何意义.有些工具可以解释这些标签
.
如果注释中包含的信息只是元数据,那么如果我错误地注释,为什么我的代码不能编译?那个特别的注释应该被忽略吧?
编辑:
仅提供一个示例... Jersey上的简单JAX-RS Web服务使用如下注释:
@Path("mypath")
Run Code Online (Sandbox Code Playgroud)
现在,如果我将其更改为:
@Paths("mypath")
Run Code Online (Sandbox Code Playgroud)
要么
@Path(123)
Run Code Online (Sandbox Code Playgroud)
它不应该阻止我根据上面的链接编译代码...
我无法定义@Result注释的'type'参数
这是我的动作注释:
@Action(value="login",
results=@Result(name="success",location="index.page", type="tiles" ))
Run Code Online (Sandbox Code Playgroud)
其中index.page是我的tile定义,我如何定义'tiles'实际上是tile结果?
在我使用struts.xml进行配置之前,我可以放在那里
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
Run Code Online (Sandbox Code Playgroud)
无论我尝试什么,我总是得到:
SEVERE: Dispatcher initialization failed
Unable to load configuration. - [unknown location]
...
Caused by: The Result type [tiles] which is defined in the Result annotation ...
could not be found as a result-type defined for the Struts/XWork package
[com.action#convention-default#] - [unknown location]
Run Code Online (Sandbox Code Playgroud) 我们应该在基于Hibernate的应用程序中使用@ org.hibernate.annotations.Entity而不是@javax.persistence.Entity吗?
或者没有这样的规则?
我有一个var的特征
trait Foo {
@Id var _id: String
}
Run Code Online (Sandbox Code Playgroud)
现在我想初始化var
class Bar(s: String) extends Foo {
_id = s
}
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
error: class Bar needs to be abstract, since variable _id in class Foo of type String
is not defined (Note that variables need to be initialized to be defined) class Bar(s: String) extends Foo {
Run Code Online (Sandbox Code Playgroud)
关键是当使用特征时,注释是继承的,我想使用它.我想有一些带有一些注释的特性用于映射,并让它们在子类中可用.
有人可以提供想法,提示,解决方案吗?
编辑:
我忘了写下extends Foo评论中提到的示例中的内容.所以这个例子是不完整的.
编辑:
当我var在Trait中定义时,@Id var _id: String = _我可以Bar简单地写入_id = s并且正确地继承了注释.但现在不必 …
symfony2应用程序具有一个Job具有类型属性的实体WebSite.
没有其他属性或方法的简化表示:
/**
* @ORM\Entity
* @ORM\Table(name="Job")
*/
class Job
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="integer", name="website_id", nullable=false)
* @ORM\ManyToOne(targetEntity="Example\ExampleBundle\Entity\WebSite")
*/
protected $website;
}
Run Code Online (Sandbox Code Playgroud)
Symfony/Doctrine试图website在持久化时将属性转换为字符串,从而导致错误:
可捕获的致命错误:类的对象示例\ ExampleBundle\Entity\WebSite无法转换为/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php第131行中的字符串
我相信上面的@ORM\Column注释表示website属性是一个整数.我不明白为什么Symfony/Doctrine希望尝试将website属性转换为字符串.
我试图解决此问题的非理想解决方法:
添加__toString()方法WebSite,返回id属性的字符串表示,导致正确的值最终在Job.website_id数据域中结束; 这种解决方法是不切实际的,因为我将来需要__toString()在应用程序的其他地方提供字符串.
@ORM\Column从website属性中删除注释; 这导致所有未来差异生成的迁移(php app/console doctrine:migrations:diff)删除相关数据库字段的"not null"方面.
我是否应对上述注释做出任何更改,以通知Symfony/Doctrine 在持久化时调用获取所需内容的getId()方法WebSite?我可以做出任何改变WebSite来实现同样的目标吗?
需要进行哪些更改才能确保上述注释可以保留在原位,以便Doctrine最终调用WebSite.getId()以获取持久存在时所需的内容,而不是尝试将对象强制转换为字符串?
我有一个类似下面的课程
public class Abcd{
private @Value("${username}")
String username;
private @Value("${password}")
String password;
public Abcd(){
ServiceService serv = new ServiceService();
Service port = serv.getServicePort();
BindingProvider bp = (BindingProvider) port;
bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username);
bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);
}
public void getSomeValueMethod(){
....
}
Run Code Online (Sandbox Code Playgroud)
那么我该如何为此编写测试呢?当我正在读取属性文件中的值时,在我尝试调用构造函数时进行测试,因为用户名和密码为null,我得到一个空指针异常,测试失败.有什么办法可以解决这个问题并成功测试吗?如何在调用构造函数之前设置这些带注释的值?
我想映射一个Map<Long, List<ItemAttribute>>内部的@Entity类,其中ItemAttribute本身是@Entity单独定义的。
这是我用于映射的代码:
@Entity
@Table(name = "ITEM_ATTRIBUTE_GROUP")
public class ItemAttributeGroup implements Cloneable, Serializable
{
@ElementCollection
@MapKeyColumn(name="groupId")
@JoinTable(name = "ATTRIBUTES_IN_GROUP", joinColumns = @JoinColumn(name = "groupId"),
inverseJoinColumns = @JoinColumn(name = "ID"))
private Map<Long, List<ItemAttribute>> attributes = new HashMap<Long, List<ItemAttribute>>();
//getters and setters........
}
Run Code Online (Sandbox Code Playgroud)
ItemAttribute是下面提到的一个单独的类:
@Entity
@Table(name = "ITEM_ATTRIBUTE")
public class ItemAttribute implements Cloneable, Serializable {
private static final long serialVersionUID = -8017036630979138942L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private Long id;
@ElementCollection // this is …Run Code Online (Sandbox Code Playgroud) annotations ×10
java ×6
hibernate ×3
jpa ×2
spring ×2
collections ×1
constructor ×1
doctrine-orm ×1
mapping ×1
mocking ×1
scala ×1
struts2 ×1
symfony-2.1 ×1
testing ×1
tiles2 ×1
traits ×1