标签: annotations

Java - 如何创建/编写自定义注释并使其工作?

如何创建自定义注释并使其工作,我不想使用"main"方法和"spring bean post processor",因为如果我在我的类中使用自定义注释,则该类应该是spring组件.

并且处理器也应该在jar中处理和搜索注释.此注释的保留是"RUNTIME",目标是"FIELD".

我在我的项目中使用了"Spring 3.1".

谢谢.

java reflection annotations

1
推荐指数
1
解决办法
921
查看次数

通过自定义注释切换GWT.create(XXX.class)

我希望你们的编码都很好。所以...我正在重构正在使用的GWT应用程序的客户端,我想知道一些事情。日复一日地寻找答案,我决定问您您的观点...

标题是相当理解的,但是有我想做的一小段。

我想改变这样的东西

public AnnotatedObject annotated = GWT.create(AnnotatedObject.class);
Run Code Online (Sandbox Code Playgroud)

通过这样的事情

@CreativeAnnotation
public AnnotatedObject;
Run Code Online (Sandbox Code Playgroud)

我不得不说,在我的xxx.gwt.xml中,我做了这样的事情:

<replace-with class="package.AnnotationObject2">
    <when-type-is class="package.AnnotationObject" />
</replace-with>
Run Code Online (Sandbox Code Playgroud)

如您所见,我的递减替换类是AnnotationObject2,目前,我在上面的替换类之间添加了一行,并且有:

<replace-with class="package.AnnotationObject1">
    <when-type-is class="package.AnnotationObject" />
    <when-property-is name="type" value="object1" />
</replace-with>

<replace-with class="package.AnnotationObject2">
    <when-type-is class="package.AnnotationObject" />
    <when-property-is name="type" value="object2" />
</replace-with>
Run Code Online (Sandbox Code Playgroud)

我不太喜欢使用xxx.html的元数据,因此我想要的结果是:

@CreativeAnnotation(type = "object2")
public AnnotatedObject;
Run Code Online (Sandbox Code Playgroud)

那么,您是否认为GWT可以实现这种事情(我不得不说我使用GWT 2.5,这是由于客户的需求引起的)吗?如果可以,您能帮我吗?

提前致谢。

编辑:我的意思是,我知道GIN ...只是想知道如何从头开始。

gwt binding annotations

1
推荐指数
1
解决办法
401
查看次数

AsyncTask方法:无论是否写"@Override"注释,我都会收到错误

我正在编写一个Android客户端,用于使用基于SOAP的Web服务.我正在使用一个AsyncTask来执行网络(即调用webservice)操作.我正在写客户端.

  1. 现在,当我尝试实施AsyncTaskdoInBackground方法,我会在下面的两个错误protected String doInBackground(String[] params)... ,如果我写@Override 与它的注释:

    MainActivity.MyTask类型的方法doInBackground(String [])必须覆盖或实现超类型方法.1快速修复:删除"@Override"注释

    以及以下错误private class MyTask extends AsyncTask...:

    MainActivity.MyTask类型必须实现继承的抽象方法AsyncTask.doInBackground(String [] ...).2快速修复:添加未实现的方法(使用 注释doInBackground再次自动生成 方法).使类型'MyTask'抽象.@Override

  2. 如果我不写@Override注释,我只得到了第二个错误.

    ...
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            String [] params = {URL, METHOD_NAME, NAMESPACE}; 
        }
    
        private class MyTask extends AsyncTask<String[], Void, String> {
    
            //@Override write it or not write it? :s ****************
            protected String doInBackground(String[] params) { …
    Run Code Online (Sandbox Code Playgroud)

java android overriding annotations android-asynctask

1
推荐指数
1
解决办法
3178
查看次数

它是如何在构造函数上运行Spring @Autowired注释的?

我正在研究Spring框架,我对此示例的构造函数的@Autowired注释有以下疑问:

@Component
public class TransferServiceImpl implements TransferService {

    @Autowired
    public TransferServiceImpl(AccountRepository repo) {
        this.accountRepository = repo;
    }
}
Run Code Online (Sandbox Code Playgroud)

究竟是什么意思呢?该AccountRepository回购对象(definied作为组分某处)自动注射到 TransferServiceImpl()构造?

它是如何运作的?是按类型完成的吗?(因为AccountRepository是Spring默认的单例),还是什么?

TNX

java spring frameworks annotations spring-annotations

1
推荐指数
2
解决办法
1957
查看次数

如何查看CrudRepository提供的findByUsername的结果

需要查看以下搜索结果:

@Repository
public interface UserRepository extends CrudRepository<User, Long> {    
    public User findByUsername(String username);        
}
Run Code Online (Sandbox Code Playgroud)

我从我的控制器那里打电话:

@RequestMapping(value = "/users/find", method = RequestMethod.GET)
public @ResponseBody User findByUsername(@RequestParam("username") String userName) {
    return usersRepo.findByUsername(userName);
}
Run Code Online (Sandbox Code Playgroud)

上述方法通过以下方式调用:

@GET("/users/find")
public User findByUsername(String userName); 
Run Code Online (Sandbox Code Playgroud)

班级用户在这里:

@Entity
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;    

    private String username;
    etc...
Run Code Online (Sandbox Code Playgroud)

通过输入例如localhost:8080/users,我可以在浏览器中看到默认方法的结果

我尝试了很多可能的请求,但似乎我错过了一些东西.任何帮助表示赞赏.

java spring annotations hibernate spring-mvc

1
推荐指数
1
解决办法
4232
查看次数

Hibernate - @OneToMany - com.sun.jdi.InvocationException发生了调用方法

我对OneToMany关系有疑问.我正在使用spring-mvc和spring-security以及hibernate 4.

我正在为所有人使用注释.

我的问题是在实体战争中,当我看到atributte List播放器调试时,我看到players= PersistentBag并且当我点击它时com.sun.jdi.InvocationException occurred invoking method.

我也得到了这个例外:

org.apache.jasper.JasperException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: clanwar.model.War.players, could not initialize proxy - no Session

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: clanwar.model.War.players, could not initialize proxy - no Session
Run Code Online (Sandbox Code Playgroud)

实体:

@Entity
@Table(name = "WARS")
public class War implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID", nullable = false)
    private int id;

    @ManyToOne …
Run Code Online (Sandbox Code Playgroud)

java annotations hibernate

1
推荐指数
1
解决办法
2万
查看次数

@Autowired发现模糊的依赖关系,仍然有效.怎么样?

为什么Spring不会抛出NoSuchBeanDefinitionException存在模糊依赖关系的地方,并且有多个bean候选者使用@Autowired注释进行自动装配?

我有这个简单的beans.xml,它有两个相同的bean,具有不同的id category,category1并且出于某种原因,Spring选择categorybean进行自动装配.我的印象是@Autowired注释byType在内部使用自动装配,因为这里有多个匹配,Spring会抛出NoSuchBeanDefinitionException异常.

我在3.2.13.RELEASE这里使用spring版本.

beans.xml中

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans 
     .....   ">

        <context:annotation-config />

        <bean id="product" class="com.study.spring.Product">
            <property name="id" value="101"/>
            <property name="name" value="Apple iPhone"/>
            <property name="active" value="true"/>
        </bean>

        <bean id="category1" class="com.study.spring.Category">
            <property name="id" value="202"/>
            <property name="name" value="Phone"/>
            <property name="active" value="true"/>
        </bean>

        <bean id="category" class="com.study.spring.Category">
            <property name="id" value="201"/>
            <property name="name" value="Communications"/>
            <property name="active" value="true"/>
        </bean>

    </beans>
Run Code Online (Sandbox Code Playgroud)

Product.java

package com.study.spring;

import org.springframework.beans.factory.annotation.Autowired;

public class Product …
Run Code Online (Sandbox Code Playgroud)

java spring annotations spring-annotations

1
推荐指数
1
解决办法
920
查看次数

JPA OneToMany ManyToOne @OneToOne或@ManyToOne引用了一个未知实体:

我只是不明白这一点.什么是以下代码,错误在哪里?我必须上课:资源和预订.资源可以有多个预留,关系是双向的.对我来说,eveything似乎找到了,我看了一堆资源和文件 - 是啊,也是在很多例子,我不能得到这个根本原因.

你们中的任何一个人都会遇到问题,或者至少有人告诉我它没有任何问题:)

package org.ademi.model;

import java.io.Serializable;
import java.util.Calendar;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Entity;

@Entity
@Table(name="Ressource")
public class Ressource implements Serializable{

    private static final long serialVersionUID = 12L;

    /**
     * A Ressource is available from a specific Date.
     */

    private Calendar availableFrom;

    /**
     * A Ressource is available until specific Date.
     */

    private Calendar availableTo;

    /** …
Run Code Online (Sandbox Code Playgroud)

java annotations hibernate jpa

1
推荐指数
1
解决办法
2万
查看次数

JPA.@ManyToOne相关字段上的@Column(updatable = false)

在我的可审计实体中,我有一个字段creationUser,我不想在db merge操作中更新.

这是我的实体代码:

@Column(updatable=false) 
@ManyToOne(cascade = CascadeType.MERGE)
public User creationUser;
Run Code Online (Sandbox Code Playgroud)

但它给了我一个错误:

Unexpected exception
PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to build EntityManagerFactory
Caused by: org.hibernate.AnnotationException: @Column(s) not allowed on a @ManyToOne property: models.AreaOfMedicine.creationUser
Run Code Online (Sandbox Code Playgroud)

那么如何防止更新该字段呢?请帮忙

annotations hibernate jpa many-to-one

1
推荐指数
1
解决办法
2402
查看次数

Spring MVC“请求处理失败” 500错误

我是Spring的新手,正在尝试学习教程。我有一个问题。我将基于xml的配置转换为基于注释的配置。是教程。

而且我收到HTTP 500错误。这是完整的堆栈跟踪。

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause


root cause

   java.lang.NullPointerException   com.ulotrix.spring.service.PersonServiceImpl.listPersons(PersonServiceImpl.java:35) 
    com.ulotrix.spring.controller.PersonController.listPersons(PersonController.java:28)    

     sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Run Code Online (Sandbox Code Playgroud)

项目结构

AppConfig.java

@EnableWebMvc
@Configuration
@ComponentScan({ "com.ulotrix.spring" })
public class AppConfig extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

@Bean
public SessionFactory sessionFactory() {

    LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource());
    builder.scanPackages("com.ulotrix.spring.model");
    builder.addProperties(getHibernationProperties());

    return builder.buildSessionFactory();
}

private Properties …
Run Code Online (Sandbox Code Playgroud)

java spring annotations hibernate spring-mvc

1
推荐指数
1
解决办法
3万
查看次数