NutzCN Logo
问答 @Inject获取spring bean 为空
发布于 2295天前 作者 qq_30fdbc0f 1988 次浏览 复制 上一个帖子 下一个帖子
标签:

nutz 集成spring data neo4j:
web.xml

<context-param>
	    <param-name>contextConfigLocation</param-name>
	    <param-value>classpath*:spring/applicationcontext-*.xml</param-value>
	</context-param>
	<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
	<listener>
		<listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
	</listener>
	
	<filter>
		<filter-name>ShiroFilter</filter-name>
		<filter-class>org.nutz.integration.shiro.ShiroFilter2</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>ShiroFilter</filter-name>
		<url-pattern>/*</url-pattern>
		<dispatcher>REQUEST</dispatcher>
		<dispatcher>FORWARD</dispatcher>
		<dispatcher>INCLUDE</dispatcher>
		<dispatcher>ERROR</dispatcher>
	</filter-mapping>

	<context-param>
		<param-name>nutz-iocby</param-name>
		<param-value>*json,ioc/,*anno,org.base,org.kg,*tx,*async,*quartz,*spring,*slog</param-value>
	</context-param>

spring 配置文件:

<?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:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://www.springframework.org/schema/context
                       http://www.springframework.org/schema/context/spring-context.xsd">

   <!-- Scan out the neo4j configuration. That configuration configures which folder
        contains persistent entities and repositories -->
   <context:component-scan base-package="org.kg.conf" />   
</beans>

spring 注解配置:

@Configuration
@EnableNeo4jRepositories(basePackages = "org.kg.repositories")
@EnableTransactionManagement
public class GranphDBConfig{

	@Bean
	public SessionFactory sessionFactory(){
		return new SessionFactory(this.configuration(), "org.kg.domain");
	}

	@Bean
	public org.neo4j.ogm.config.Configuration configuration(){
		ConfigurationSource properties = new ClasspathConfigurationSource("properties/ogm.properties");
		org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration.Builder(properties).build();
		return configuration;
	}

	@Bean
	public Neo4jTransactionManager transactionManager(){
		return new Neo4jTransactionManager(this.sessionFactory());
	}
}

domain:

@NodeEntity(label = "Class")
public class Course {

	private Long id;

	private String name;

	@Relationship(type = "SUBJECT_TAUGHT")
	private Subject subject;

	@Relationship(type = "TEACHES_CLASS", direction = Relationship.INCOMING)
	private Teacher teacher;

	@Relationship(type = "ENROLLED", direction = Relationship.INCOMING)
	private Set<Student> students = new HashSet<>();

	public Long getId() {
		return id;
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public String getName() {
		return name;
	}

	public Subject getSubject() {
		return subject;
	}

	public Set<Student> getStudents() {
		return students;
	}

	@Override
	public String toString() {
		return "Course{" +
				"id=" + getId() +
				", name='" + name + '\'' +
				", teacher=" + teacher +
				", subject=" + subject +
				", students=" + students.size() +
				'}';
	}

	public void updateFrom(Course course) {
		this.name = course.name;
	}
}

CourseRepository:

import org.shujuhai.kg.domain.Course;
import org.springframework.data.repository.CrudRepository;

public interface CourseRepository extends CrudRepository<Course, Long> {

}

CourseController:

@At("/api/classes")
@Ok("json")
@Fail("json:" + Constant.GLOBAL_FAIL_JSON)
public class CourseController{

	@Inject
	private CourseRepository courseRepository;

	@At
	public Iterable<Course> readAll(){
		return courseRepository.findAll();
	}

	@At
	public Course create(@Param("..") Course course){
		return courseRepository.save(course);
	}

	@At
	public Course read(@Param("id") Long id){
		//courseRepository = Mvcs.ctx().getDefaultIoc().get(CourseRepository.class);
		return courseRepository.findById(id).orElseThrow(NotFoundException::new);
	}

	@At
	public void delete(@Param("id") Long id){
		courseRepository.deleteById(id);
	}

	@At
	public Course update(@Param("id") Long id, @Param("..") Course update){
		final Course existing = courseRepository.findById(id).orElseThrow(NotFoundException::new);
		existing.updateFrom(update);
		return courseRepository.save(existing);
	}
}

在CourseController中通过注解@Inject注入的courseRepository为空,(https://github.com/nutzam/nutzmore/tree/master/nutz-integration-spring#如何在nutz-mvc环境下-通过nutioc的api获取spring-ioc的bean)说可以通过@Inject拿到spring bean 的值,不知是哪个环节出问题了?求大神指点。

3 回复

CourseController加@IocBean

@wendal 哎呀呀呀呀,这失误确实不应该,加上果然好了,谢谢大神!

添加回复
请先登陆
回到顶部