我的春豆不能正常工作?

时间:2016-04-18 09:20:49

标签: java xml spring applicationcontext spring-bean

我为pojo类中的某些方法获取了null值,其他方法工作正常。 这是我的POJO班Student.java

public class Student {
private int id;
private String name;
private String message1;
private String message2;
private String message3;


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;
}

public void init()
{
    System.out.println("Bean is started");
}

public void destroy()
{
    System.out.println("Bean is destroyed");
}
public String getMessage1() {
    return message1;
}
public void setMessage1(String message1) {
    this.message1 = message1;
}
public String getMessage2() {
    return message2;
}
public void setMessage2(String message2) {
    this.message2 = message2;
}
public String getMessage3() {
    return message3;
}
public void setMessage3(String message3) {
    this.message3 = message3;
}
}

这是我的xml文件beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="student" class="Student">
    <property name="id" value="2342"></property>
    <property name="name" value="RamaKrishna"></property>
    <property name="message1" value="Student--message1"></property>
    <property name="message3" value="Student--message3"></property>
</bean>
</beans>

这是我的主要方法逻辑,Logic.java

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Logic{
public static void main(String args[]){
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

    Student student = (Student)context.getBean("student");

    System.out.println(student.getId());
    System.out.println(student.getName());
    System.out.println(student.getMessage1());
    System.out.println(student.getMessage3());
}
}

我正在获取第3和第4个打印语句的输出空值。 我的输出是:

2342
RamaKrishna
null
null

我应该得到像

这样的输出
2342
RamaKrishna
Student--message1
Student--message3

请帮助理解为什么这样做。

1 个答案:

答案 0 :(得分:-1)

您的代码运行正常。我的输出:

angular.module('tabsDirectiveModule', [])
.directive('tab', function() {
return {
      restrict: 'E',
      transclude: true,
      template: '<div role="tabpanel" ng-show="active" ng-transclude></div>',
      require: '^tabset',
      scope: {
        heading: '@',
        id: '@',
                setThumb: '&'
      },
      link: function(scope, elem, attr, tabsetCtrl) {
        scope.active = false;
        tabsetCtrl.addTab(scope, attr.id); 
              }
    };
})
.directive('tabset', function() {
return {
restrict: 'E',
transclude: true,
scope: { },
templateUrl: 'UI/templates/tabset.html',
bindToController: true,
controllerAs: 'tabset',
controller: function() {
  var self = this;
  self.tabs = [];
  self.addTab = function addTab(tab, id) {
      tab.setThumb({arg: id});
      self.tabs.push(tab);
       if(self.tabs.length === 1) {
        tab.active = true;
      }
    };
  self.select = function(selectedTab) {
      angular.forEach(self.tabs, function(tab) {
        if(tab.active && tab !== selectedTab) {
          tab.active = false;
        }
      });
      selectedTab.active = true;

    };
}
};
});

尝试重新编译代码