Solution of injecting mapper null pointer in service layer

  • 2021-09-20 20:19:20
  • OfStack

Injecting mapper null pointer in service layer

Today, I encountered an extremely tricky problem. I didn't talk too much about the code and test the unit first


@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBoot_Run.class)
@ContextConfiguration(locations = { "classpath:mybatis/mappers/RevMapper.xml" })
public class TestTransaction {
 @Autowired
 RevMapper remapper;
 @Test
 public void testInsert() {
  ReData data = new ReData();
  data.setReTime(new Date()).setSeID("fdewfcdsfdssdfdsf").setSendDate(new Date());
  remapper.insertObject(data);
 }

Then there is the service code


public class ReService {
 
 @Autowired
 private RevMapper reMapper;
 private Socket socket=null;
 private BufferedReader br=null;
 private PrintWriter pw=null;
 public void recevice() {
  try {
    // Create a server , And open 3081 Port 
      ServerSocket serv

RevMapper class in the test injected well, for the hair in service is empty, 1 straight empty, empty empty! ! !

I have added the annotations of @ mapperScan and @ mapper on the Internet once. Why? ! ! ! !

In the CSDN of all the great gods in Expo, I found that everyone copied it over and over, and my younger brother admired it! !

Solve it! ! !

Because I wrote this in the startup class


@SpringBootApplication(exclude=DataSourceAutoConfiguration.class)
@MapperScan( " cn.yungtay.mapper " )
public class SpringBoot_Run {
public static void main(String[] args) {
 SpringApplication.run(SpringBoot_Run.class, args);
 ReMapper re=new ReMapper();
 re.receive;
}
}

Don't spray the powerful Oba, my first reaction is like this! !

Problem comes out, when an object is new comes out, he is not to spring management, so the object can not be injected, null is a matter of course

The second question, you want a method to start with the main starter class, you can do this


@Service
public class ReService implements ApplicationRunner{
@Autowired
private RevMapper reMapper;
private Socket socket=null;
 . . . . . . . . . . . . . 
@Override
public void run(ApplicationArguments args) throws Exception {
 // TODO Auto-generated method stub
  The method you need to start XXXXXXXX
}

I feel that I have gained 1 point of wisdom again!

springmvc common class (non-control, service) injects mapper into null

When writing a timer to the project, you need to use the injection mapper for database operation, like the injection in serviceimpl


@Autowired
UserMapper usermapper; 

Invalid. After debug, usemapper was found to be null, indicating that the injection was not successful

After reading other articles, I know that thread from new is not in the container of spring, so I can't inject it successfully and get bean

However, according to his method, it is still null, and his idea is to actively inject bean, which should be correct.

But I may be a little special, so I can only use the ultimate solution in the end


ApplicationContext  ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
usermapper = (UserMapper) ac.getBean("UserMapper");
usermapper.deleteAllCookies();

Don't forget to give mapper a name, for example


@Repository(value="UserMapper")
public interface UserMapper {
public List<User> selectByExample(@Param("username1")String username,@Param("password")String password);
public int insertToken(@Param("username1")String username,@Param("token")String token);
public String checkToken(String token);
public int logout(@Param("username1")String username,@Param("token")String token);
public int deleteAllCookies();
}

This method does not feel very good subjectively, so let's do it first!


Related articles: